Thursday, June 13, 2013

[android help] getActivity() returns null in fragment when i override onBackPressed in parent Activity


getActivity() returns null in fragment when i override onBackPressed in parent Activity



I have an Activity with few tabs manager by ViewPager. And there is one tab where i'd like to prevent closing activity when users click BACK button. Instead it loads upper level of data.


So my code in FragmentActivity looks like this



@Override
public void onBackPressed() {
boolean allowQuit = true;

if (mTabsAdapter != null){
if(mTabsAdapter.getItem(mTabsAdapter.selectedTab) instanceof backPressOverrider){
allowQuit = ((backPressOverrider)mTabsAdapter.getItem(mTabsAdapter.selectedTab)).onCustomBackPressed();
}
}

if (allowQuit) {
super.onBackPressed();
}
}


And it works - activity not finished and I see my fragment, but getActivity() returns null. None of onDetach,onStop, onDestroy,.. was fired in fragment.


Is there any way to overcome this problem?



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...