In my application, I have 3 fragments.
The application starts with [1], the user can go only to [2], and then additionally to [3].
Since [3] is deep down, I want [3] to go back to [1].
I am currently calling [2] using addToBackStack (null). Since I do not call addToBackStack in [3], I assumed that it would return to [1].
It happens that [3] returns to [1], but now both fragments are displayed overlapping. Fragment [3] does not call onPause (). Calling the fragment [2] again displays [2] on top of the rest, without clearing the screen.
Navigating back and forth will crash the application. Sometimes when opening a new fragment, sometimes when you click the "Back" button. Always with the error "Fragment already added" (which is very unusual for the "Back" button, but I check this also before switching fragments).
Any ideas that might cause this weird behavior? Using addToBackStack or [3] also fixes the problem, but does not solve my requirement. I use all the super methods if necessary.
Information: Sample code for downloading at http://beadsoft.de/android/FragmentTest.zip
Same behavior on 2.x, 4.2.2. Using ActionBarCompat.
Code for adding a fragment:
FragmentManager fm = getActivity().getSupportFragmentManager();
Fragment fragment = fm.findFragmentByTag(Fragment_xyz.FRAG_TAG);
if (fragment == null)
fragment = Fragment_xyz.newInstance(int data);
fm.beginTransaction().replace(R.id.container, fragment, Fragment_xyz.FRAG_TAG)
.addToBackStack(null).commit()