popBackStack() after addToBackStack does not work

后端 未结 6 1254
梦毁少年i
梦毁少年i 2021-02-11 20:43

My project contains two fragment :

  • FragmentA : the fragment loaded by default when the app starts
  • FragmentB : replace the fragmentA when a c
相关标签:
6条回答
  • 2021-02-11 20:46

    The problem is you're mixing Fragment and methods from the support library.

    If you are using the support library, make sure:

    • your Activity extends from the android.support.v4.app.FragmentActivity
    • your Fragment extends from android.support.v4.app.Fragment
    • use getSupportFragmentManager() to get the android.support.v4.app.FragmentManager

    Your code in the Activity would be:

    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
       getSupportFragmentManager().popBackStackImmediate();
    }
    

    Please be aware that if you would like to get the FragmentManager from the Fragment code, you have to use the getFragmentManager method, as explained in the documentation (probably that's the cause of some confusion if you don't have much experience).

    If you are not using the support library:

    • your Activity extends from the android.app.Activity
    • your Fragment extends from android.app.Fragment
    • use getFragmentManager() to get the android.app.FragmentManager

    Your code would be:

    if (getFragmentManager().getBackStackEntryCount() > 0) {
       getFragmentManager().popBackStackImmediate();
    }
    

    fragmentTransaction.commit(); is not necessary in both cases, so remove it.

    Also, please call fragmentTransaction.addToBackStack(null); just before the commit but after the other operations.

    0 讨论(0)
  • 2021-02-11 20:47
    The problem is in the order of your code. Here are two things you need to pay attention to.
    1. You need to use addToBackStack() after your adding, replacing fragment. Then commmit()

    2. Then popBackStackImmediate can reverse the operation and it is working. I hope it solves the problem. I know it is an old post but I do encounter a similar problem and wish this update can help others. Below should be the correct order:

      FragmentTransaction fragmentTransaction =getSupportFragmentManager().beginTransaction();

      fragmentTransaction.replace(R.id.main_fragment_container, new FragmentB());
      
      fragmentTransaction.addToBackStack(null);
      
      fragmentTransaction.commit();
      
    0 讨论(0)
  • 2021-02-11 20:57

    I will suggest using the fragment replacement with Tag to produce the same result .

    Add the fragment B to activity with tag

    fragmentTransaction.replace(R.id.main_fragment_container, new FragmentB(),"TAG_B");
    

    Fragment A -> Fragment B [onBackPressed] -->Fragment A Override the onBackPressed() in the Activity files where ,

    // check for fragment B and you are viewing fragment B

    if (getSupportFragmentManager().findFragmentByTag("TAG_B")!=null)  
    {
        fragmentTransaction.replace(R.id.main_fragment_container, new FragmentA(),"TAG_A");
    }
    

    addToBackStack("TAG") and popBackStackImmediate("TAG") always revert to fragment condition without any data in the UI right before fragment is created or added to activity !

    0 讨论(0)
  • 2021-02-11 20:58

    You should call

    fragmentTransaction.addToBackStack(null);
    

    after performing all operations such as add(), remove(), and replace() and Just before commit(). Only then this transaction will be added to backstack. Only then you will be able to return to previous fragment state with Back button. Details here.

    0 讨论(0)
  • 2021-02-11 21:07

    You use the getSupportedFragmentManager() to replace FragmentA by FragmentB. But you call popBackStack() on the getFragmentManager().

    If you are adding the Fragments to the android.support.v4.app.FragmentManager you also have to call popBackStack() on the same FragmentManager.

    This code should solve the problem:

    if (getSupportFragmentManager().getBackStackEntryCount() > 0){
        boolean done = getSupportFragmentManager().popBackStackImmediate();
    }
    
    0 讨论(0)
  • 2021-02-11 21:09

    If you are Struggling with addToBackStack() and popBackStack() then simply use

    FragmentTransaction ft =getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_frame, new HomeFragment(), "Home");
    ft.commit();`
    

    In your Activity In OnBackPressed() find out fargment by tag and then do your stuff

    Fragment home = getSupportFragmentManager().findFragmentByTag("Home");
    
    if (home instanceof HomeFragment && home.isVisible()) {
        // do you stuff
    }
    

    For more Information https://github.com/DattaHujare/NavigationDrawer I never use addToBackStack() for handling fragment.

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