popBackStack() after addToBackStack does not work

后端 未结 6 1264
梦毁少年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 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();
    }
    

提交回复
热议问题