How to delete a specific fragment from back stack in android-ThrowExceptions

Exception or error:

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 Fragment-3.

Calling order; Fragment-1–>Fragment-2–>Fragment-3.

When Fragment-3 is on the screen and then back button is clicked, i want to go

Fragment-1.That means i want to delete Fragment-2 from back stack.

How to do this ?

How to solve:

In the backstack you don’t have Fragments, but FragmentTransactions. When you popBackStack() the transaction is applied again, but backward. This means that (assuming you addToBackStackTrace(null) every time) in your backstack you have


If you don’t add the second transaction to the backstack the result is that your backstack is just


and so pressing the back button will cause the execution of 2->1, which leads to an error due to the fragment 2 not being there (you are on fragment 3).
The easiest solution is to pop the backstack before going from 2 to 3

//from fragment-2:
   .replace(, fragment3)

What I’m doing here is these: from fragment 2 I go back to fragment 1 and then straight to fragment 3. This way the back button will bring me again from 3 to 1.


I had a very similar scenario to yours, my solution was just checking the amount of backStack transactions that I had.

If the transactions where more than 0 then I would simply pop it right away
so it would skip it when pressing back.

if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
fragmentTransaction.replace(, newFrag, MAIN_FRAGMENT_TAG);

This would successfully:

  • A -> B (back pressed) -> back to A

  • A -> B -> C (back pressed) -> back to A

The only downside that I see is that there is a quick flash where fragment A is displayed before going to fragment C.


If you are adding/launching all three fragments in the same activity, instead of the add() method of FragmentTransaction for showing Fragment3, use the replace() method of FragmentTransaction (replace Fragment2 with Fragment3). The replace method removes the current fragment from backstack before adding the new fragment. If you are launching Fragment3 from a different activity, and thus you can’t/don’t want to use replace(), remove Fragment2 from backstack before starting the new activity (which adds fragment3):

// in Fragment2, before adding Fragment3:
FragmentManager fragmentManager = getFragmentManager();
               .remove(this) // "this" refers to current instance of Fragment2
// now go ahead and launch (add) fragment3
// if fragment3 is launched from a different activity, 
// start that activity instead
               .add(, new Fragment3(),


Code for Fragment A -> Fragment B:

Add Fragment A in BackStack of Fragments

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(, new fragmentB());

Code for Fragment B -> Fragment C:

Do not Add Fragment B in BackStack of Fragments

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(, new fragmentC());

It will works in this way: A -> B -> C and while returning C-> A as you excepted.

Hope it will help you.


You add to the back state from the FragmentTransaction and remove from the backstack using FragmentManager pop methods:

FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();


Simply you can skip the fragment from adding into the fragment stack so when you come back from Fragment-3 it will come back to Fragment-1

Updated :

To disable the animation, you need to override the onCreateAnimation method…

public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    if (disableAnim) {
        Animation a = new Animation() {};
        return a;
    return super.onCreateAnimation(transit, enter, nextAnim);


   for (int i = 0; i < mFragmentManager.getBackStackEntryCount(); i++) {
        currentFragment = mFragmentManager.getFragments().get(i);
        if (!(currentFragment instanceof ParticularFragment))

Leave a Reply

Your email address will not be published. Required fields are marked *