My project contains two fragment :
fragmentA
when a c
The problem is in the order of your code. Here are two things you need to pay attention to.
You need to use addToBackStack() after your adding, replacing fragment. Then commmit()
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();