0

I have an Activity that is holding 5 fragments. One of those fragments is holding 5 more fragments. if i add to the fragmentManager a .addToBackStack(null). the back button returns to the last fragment from the activity and not to the last fragment from the "father" fragment (that is holding 5 more fragments).

Any help please..

EDIT:

ACTIVITY:

fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().
            replace(mainContent.getId(), currentFragment)
            .addToBackStack(null)
            .commit();


FRAGMENT:

fragmentManager = getChildFragmentManager();
fragmentManager.beginTransaction().
            replace(mainContent.getId(), currentFragment)
            .addToBackStack(null)
            .commit();
ilan
  • 4,402
  • 6
  • 40
  • 76
  • Make sure all your `FragmentTransactions` are from the same FragmentManager. You're probably using two different fragment managers at the moment. – prettyvoid Oct 16 '14 at 19:05
  • I am i have a activity.getFragmentManager. and is the fragment i am using getChildFragmentManager. – ilan Oct 16 '14 at 19:06
  • i switched to the same fragment manager and then i get at the fragments Childs a null when i call getParentFragment – ilan Oct 16 '14 at 19:11
  • So you're using two Fragment Managers. What's the code you're calling on your 'back' inside the Fragment with child fragments? Also do you mean the physical back button? – prettyvoid Oct 16 '14 at 19:12
  • i don't have any code in the "back" i just added to the transaction addToBackStack(null). and yes i mean the physical button – ilan Oct 16 '14 at 19:14
  • Share the code for your activity and fragment, at least the parts where you're creating fragment transactions and committing. – prettyvoid Oct 16 '14 at 19:51
  • I think what you're facing is a bug in Android SDK. See my post – prettyvoid Oct 16 '14 at 20:27

2 Answers2

1

This is probably the bug mentioned here: https://code.google.com/p/android/issues/detail?id=40323

You can workaround it by handling the 'back' manually. Refer to this thread for a lot of workarounds: Android 4.2: back stack behaviour with nested fragments

Community
  • 1
  • 1
prettyvoid
  • 3,446
  • 6
  • 36
  • 60
-1

I think the issue might be with "the fragmentManager" here. There is more than one FragmentManager.

There is the FragmentManager for the ActivityActivity.getFragmentManager().

And there is the FragmentManager for child Fragments within a FragmentFragment.getChildFragmentManager() or Fragment.getFragmentManager().

Let's say you have an activity, a parentFragment and a childFragment. So, instead of activity.getFragmentManager(), I think in your case, you might want either childFragment.getFragmentManager() or parentFragment.getChildFragmentManager().

Note that if you are using the Support package, the names may be different.

Leo Landau
  • 1,785
  • 17
  • 25