How to delete a specific fragment from back stack

2019-02-04 15:53发布

问题:

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 ?

回答1:

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

1->2
2->3

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

1->2

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:
getFragmentManager().popBackStack();
getFragmentManager().beginTransaction()
   .replace(R.id.container, fragment3)
   .addToBackStack(null)
   .commit();

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.



回答2:

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) {
        getSupportFragmentManager().popBackStackImmediate();
}
...
fragmentTransaction.replace(R.id.main_fragment, newFrag, MAIN_FRAGMENT_TAG);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();

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.



回答3:

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();
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();


回答4:

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();
trans.remove(myFrag);
trans.commit();
manager.popBackStack();

OR

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

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


回答5:

Code for Fragment A -> Fragment B:

Add Fragment A in BackStack of Fragments

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout_container, new fragmentB());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

Code for Fragment B -> Fragment C:

Do not Add Fragment B in BackStack of Fragments

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout_container, new fragmentC());
fragmentTransaction.commit();

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

Hope it will help you.



回答6:

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