popBackStack() after addToBackStack does not work

后端 未结 6 1685
野趣味
野趣味 2021-02-11 20:29

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:09

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

提交回复
热议问题