I have one activity, ActivityA
, and 3 fragments, FragmentA
, FragmentB
, and FragmentC
.
To add FragmentA, I use replace
, where fragment
is a new instance of FragmentA
:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_layout_fragment, fragment)
.commit();
In the onCreate
method of ActivityA, I do a check for FragmentA
creation:
if (savedInstanceState == null) {
FragmentA fragmentA = FragmentA.newInstance();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_layout_fragment, fragmentA)
.commit();
}
To add FragmentB
and FragmentC
, I use add
and addToBackStack
, where fragment
is either a new instance of FragmentB
or FragmentC
:
getSupportFragmentManager()
.beginTransaction()
.add(R.id.frame_layout_fragment, fragment)
.addToBackStack(null)
.commit();
I press the back button while on FragmentC
, it shows FragmentB
, and pressing the back button while on FragmentB
shows FragmentA
, as expected. While I was on FragmentC
, I rotate the device, and FragmentC
still shows, as expected.
However, when I navigate from FragmentC
to FragmentA
using the 2 presses of the back button, then rotate the device twice, portrait -> landscape -> portrait, add FragmentB
, add FragmentC
, then I rotate the device once, I expected FragmentC
to show, but instead FragmentB
shows. When I press the back button once, nothing happens. When I press it again, it navigates back to FragmentA
. It seems like FragmentC
is present in the back stack, but for some reason its not visible.
Why is FragmentC
not visible in this scenario?