This is my current solution for anyone interested.
In the function of adding a new Fragment :
final Fragment toRemove = fragmentManager.findFragmentById(containerID); if (toRemove != null) { new Handler().postDelayed(new Runnable() { @Override public void run() { fragmentManager.beginTransaction().hide(toRemove).commit(); } }, getResources().getInteger(android.R.integer.config_mediumAnimTime) + 100);
and in onCreate :
final FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.addOnBackStackChangedListener( new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { Fragment current = fragmentManager.findFragmentById(containerID); if (current != null && current.isHidden()) { fragmentManager.beginTransaction().show(current).commit(); } } });
I would prefer some kind of AnimationListener instead of the handler above, but I did not see that you could use it to detect the end of a transaction animation that was not bound to a fragment, for example onCreateAnimation() . Any suggestions / corrections with the appropriate listener will be appreciated.
I’ll point out that Fragment I’m adding this method, it’s easy, so there’s no problem for me to contain them in the fragment container along with the fragment that they are on top of.
If you want to remove the fragment, you can put fragmentManager.beginTransaction().remove(toRemove).commitAllowingStateLoss(); in Handler Runnable , and in OnBackStackChangedListener :
// Use back stack entry tag to get the fragment Fragment current = getCurrentFragment(); if (current != null && !current.isAdded()) { fragmentManager.beginTransaction() .add(containerId, current, current.getTag()) .commitNowAllowingStateLoss(); }
Please note that the above solution does not work for the first fragment in the container (since it is not in the background stack), so you should have another way to restore it, maybe keep a link to the first fragment anyway ... But if you don’t use the back stack and always replace fragments manually, this is not a problem. OR you can add all the fragments to the back stack (including the first one) and redefine onBackPressed to make sure your activity is complete and not display a blank screen when only one fragment remains in the back stack.
EDIT: I found the following functions that could replace FragmentTransaction.remove() and FragmentTransaction.add() above:
FragmentTransaction . detach () :
Disconnect this fragment from the user interface. This is the same state as when stacked on the back stack: the fragment is deleted from the user interface, however, its state is still actively controlled by the fragment manager. Upon entering this state, its hierarchy of representations is destroyed.
FragmentTransaction . attach () :
Reattach the fragment after it has previously been disconnected from the user interface with disconnection (fragment). This will cause its view hierarchy to be recreated, attached to the user interface, and displayed.