popBackStack() after addToBackStack does not work

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

提交回复
热议问题