I have three fragment A, B and C
This is the path : A -> B -> C : A go to B and B go to C
When I go back from the fragment C , I want to go to the fragment A.
- A <- C : C go back to the root fragment A
- A <- B : B go back to the root fragment A
But My problem is when I pressed back in the fragment C , I get this behviour : It seems that the fragment C is not cleared from the backstack :
As for my code , this is the method of the replaceFragment :
public void replaceFragment(Fragment fragment, boolean withBackStack) { android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); String fragmentTag = fragment.getClass().getName(); fragmentTransaction.replace(R.id.frame, fragment, fragmentTag); if (withBackStack) fragmentTransaction.addToBackStack(fragmentTag); try { fragmentTransaction.commitAllowingStateLoss(); } catch (IllegalStateException ex) { ex.printStackTrace(); } }
First I called replaceFragment(new AFragment(), false);
in the MainActivity
, Then in the Fragment A when I clicked in the button, I called mListener.replaceFragment(new BFragment(), true);
Finally , in the Fragment B when I clicked the button , I called mListener.replaceFragment(new CFragment(), false);
Does anyone have an explanation for this behaviour ? The last fragment C shouldn’t be cleared when I click backpressed ?
Here , you will find the whole example. Thanks in advance!