Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 960
Sunny Avatar asked Apr 06 '14 07:04

Sunny


People also ask

How can I change fragments in Android?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();

How do I add a fragment to the top of activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

What is Backstack in fragments?

Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.


1 Answers

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.

like image 62
Sunny Avatar answered Oct 21 '22 00:10

Sunny