Going from 3rd to 1st fragment in the backstack: Android

Sunny picture Sunny · Apr 6, 2014 · Viewed 8k times · Source

I have a sequence of event via which i have added three fragments to the backstack, one by one. Each of these fragments covers the full screen of the activity.

I have stored the is returned from the commit of Frag1.

Now in Frag3, based on a specific click, I want to go back to Frag1 directly and discard/pop all Fragments in between.

So, when this button is clicked i send a message to the activity which does the following:

getSupportFragmentManager().popBackStack(mFrag1Id, FragmentManager.POP_BACK_STACK_INCLUSIVE);

But i just got a blank screen, so i assume no fragment was loaded.

I even tried: In commit - fragmentTransaction.addToBackStack("Fragment1"); and then

getSupportFragmentManager().popBackStack("Fragment1", FragmentManager.POP_BACK_STACK_INCLUSIVE);

But it doesn't work. Could someone please help me with this?

Thanks.

Answer

Sunny picture Sunny · Apr 6, 2014

OK so I found the issue.

FragmentManager.POP_BACK_STACK_INCLUSIVE pops all the fragments including the one whose id passed as argument.

SO for example:

getSupportFragmentManager().popBackStack(mFrag1Id, FragmentManager.POP_BACK_STACK_INCLUSIVE);

Here it will pop everything on the stack including fragment whose id id mFrag1Id.