I have a problem about removing a specific fragment from back stack.My scenario is like this.Fragment-1 is replaced with Fragment-2 and then Fragment-2 is replaced with Frag
If you are adding/launching all three fragments in the same activity, instead of the add()
, use replace()
(replace Fragment2
with Fragment3
). The replace
method removes the current fragment from backstack before adding the new one. If you are launching Fragment3
from a different activity, and thus you can't use replace()
, remove Fragment2
from backstack before starting the new activity (which adds Fragment3
):
// in Fragment2, before adding Fragment3:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.remove(this) // "this" refers to current instance of Fragment2
.commit();
fragmentManager.popBackStack();
// now go ahead and launch (add) fragment3
// if fragment3 is launched from a different activity,
// start that activity instead
fragmentManager.beginTransaction()
.add(R.id.a_container_view_in_activity, new Fragment3(),
Fargment3.FRAGMENT3_ID)
.commit();