0

This seems to be weird problem,

Scenario:

I have a FragmentActivity (FragActy) - having which has TabHost FragActy starts a child Fragment (child_Fragment_1) childFrag_1 starts another child (child_Fragment_2) childFrag_2 Starts A new Activity (newActivity) - which is a simple android activity.

good so far,

But when I click back from newActivity instead of showing child_Fragment_2, Im going to child_Fragment_1.

The problem is weird because, when in lastActivity, if I change screen orientation Once and then press back the behavior is normal, i.e, child_Fragment_1 is shown. Again if I rotate twice I'm going to child_Fragment_1.

Image for scenario description below.

Thanks!

enter image description here

vijeth.ag
  • 515
  • 5
  • 8
  • See the following link, You can understand how to communicate between Activity - Fragment and Fragment - Fragment http://stackoverflow.com/questions/24321449/android-navigation-drawer-unable-to-settext-in-a-fragment-from-mainactivity/24322359#24322359 – Umang Kothari Oct 13 '14 at 13:06

1 Answers1

0

Try this, clear the fragment stack when you are on fragment 2 using this

FragmentManager fm = getActivity().getSupportFragmentManager();
   for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
    fm.popBackStack();
}

and before launching the fragment activity add the fragment to backstack like this:

ft.addToBackStack(null);

hope it helps :)

Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47
  • you wellcome :) hope it helps – Syed Raza Mehdi Oct 14 '14 at 05:18
  • Hi Syed, Thanks for the reply. I didn't seem to understood though, U mean When i come to childFragment_2 clear off the fragment backstack? Thanks(Sorry hit enter it got posted :P) – vijeth.ag Oct 14 '14 at 05:19
  • yes here is what you have to do before you launch your fragment activity : 1) clear the stack 2) push your desired fragment which you wana show on back press into stack 3) start the activity – Syed Raza Mehdi Oct 14 '14 at 05:52
  • Ya I will try your solution, meanwhile I tried recreating project with similar setup with similar fragmentActivity and child fragments, Seems to be working fine - even without handling onSaveInstances anywhere, wherein actual project has onSaveinstances handled. – vijeth.ag Oct 14 '14 at 06:53