I have put together a simple program that uses fragment replacement with a single activity and two fragments. One fragment has a button, that when pressed, replaces the frag
@Emanuel is correct that the UI state is retained across screen rotations. However what's happening is simply that your Activity's onCreate is always called on rotation, which always adds your startingFragment
The solution is simple. Just add a condition in your onCreate as so to handle the case when it's called as part of the activity lifecycle:
if (savedInstanceState == null)
{
FragmentTransaction t = getFragmentManager().beginTransaction();
t.add(R.id.fragment_container, startingFragment, "start").commit();
}
You might want to consider doing
t.replace(R.id.fragment_container, startingFragment, "start").commit();
though instead of
t.add(R.id.fragment_container, startingFragment, "start").commit();