This works perfectly when the fragment replacing the currentFragment is of different types. However for some strange reason, if I try to switch in another fragment of type FragmentB when there is one currently added, the back stack suddenly stops working. Instead of replacing all fragments within the linearlayout, the old fragment is placed below the current one.

Here is how I call the back stack to pop. It is done this way because the onBackPressed() method does not handle backstacks on nested fragments by default.

I have managed to make my app work if I simply replace my onBackPressed method with the following, however I would really like to know what is happening, and why it only happens when I am replacing the fragment with one of the same type.