In an activity there is a fragment that contains a list of objects. This fragment should be replaced by another fragment that contains details of a list item. When the detail fragment is active and the back button is pressed then the listfragment should be resumed.
my code:
@Override
public void onBackPressed() {
if (detailsFragment.isVisible())
{
goToListFragment();
}
}
}
public void goToListFragment() {
String backStateName = listFragment.getClass().getName();
FragmentManager fm = getSupportFragmentManager();
boolean fragmentPopped = fm.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.main_fragment_container, listFragment);
ft.addToBackStack(backStateName);
ft.commit();
}
}
public void goToDetailsFragment(String foodAsJSON) {
String backStateName = detailsFragment.getClass().getName();
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_fragment_container, detailsFragment)
.addToBackStack(backStateName)
.commit();
}
what I observe:
listfragment: onPause()
<back pressed in details view>
listfragment: oncreateView()
listfragment: onResume()
So, when the back button is pressed I would expect that only the onResume function is called. Actually that how its described in the fragment lifecycle. But as I have observed the oncreateView() is called too. That causes the fragment to reload completely and that's not the wanted behavior. So, how do I resume the master fragment properly?