Android Fragment Back button overlays another fragment and remains active

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()
0
source share
2 answers

, . . :

LifeCycle of the App
start
04-07 20:34:46.115: D/Fragment_1(15309): onCreateView Fragment_1
04-07 20:34:46.115: D/Fragment_1(15309): onResume Fragment_1
Click to open 2
04-07 20:34:49.148: D/Fragment_1(15309): onPause Fragment_1
04-07 20:34:49.148: D/Fragment_2(15309): onCreateView Fragment_2
04-07 20:34:49.148: D/Fragment_2(15309): onResume Fragment_2
Click to open 3
04-07 20:34:53.633: D/Fragment_2(15309): onPause Fragment_2
04-07 20:34:53.633: D/Fragment_3(15309): onCreateView Fragment_3
04-07 20:34:53.633: D/Fragment_3(15309): onResume Fragment_3
back Button. Since 3 is not on backstack, app returns to 1 (not pausing 3)
Here Frag 1 and 3 are running and displayed overlaying!
04-07 20:35:03.653: D/Fragment_1(15309): onCreateView Fragment_1
04-07 20:35:03.653: D/Fragment_1(15309): onResume Fragment_1
back Button on 1 - App exit
04-07 20:35:23.474: D/Fragment_1(15309): onPause Fragment_1
04-07 20:35:23.474: D/Fragment_3(15309): onPause Fragment_3

Google: https://code.google.com/p/android/issues/detail?id=68160

, FragmentManager .

0

? :

MyNewFragment f=new MyNewFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, f).commit();

Try:

MyNewFragment f=new MyNewFragment();
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container, f).commit();
0

All Articles