How to delete a specific fragment from back stack in android

后端 未结 6 787
野趣味
野趣味 2020-12-29 05:09

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

相关标签:
6条回答
  • 2020-12-29 05:34

    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);
    }
    
    0 讨论(0)
  • 2020-12-29 05:38

    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();
    
    0 讨论(0)
  • 2020-12-29 05:39
       for (int i = 0; i < mFragmentManager.getBackStackEntryCount(); i++) {
            currentFragment = mFragmentManager.getFragments().get(i);
            if (!(currentFragment instanceof ParticularFragment))
                mFragmentManager.popBackStack();
            else
                break;
        }
    
    0 讨论(0)
  • 2020-12-29 05:41

    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.

    0 讨论(0)
  • 2020-12-29 05:44

    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.

    0 讨论(0)
  • 2020-12-29 05:47

    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.

    0 讨论(0)
提交回复
热议问题