Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PopBackStack but keep the first fragment in android

I am working on fragment transaction, and the backstack is like this:

fragA => fragB => fragC => fragD

I would like to return to fragA after back fromn the fragD

fragD => onBackPress => fragA

So, I tried code like:

getChildFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

but it clear all the backstack , how can I keep the first fragment in the backstack? Thanks a lot

like image 577
user782104 Avatar asked Feb 06 '15 07:02

user782104


People also ask

Does popBackStack remove fragment?

popBackStack() will remove current fragment and replace it with the last one from the stack. For this to work you need to do addToBackstack() on the last fragment transaction.

How can I maintain fragment state when added to the back stack?

add() - (create and) add a Fragment B and it overlap Fragment A, which is still active in the background. remove() - can be used to remove Fragment B and return to A. Fragment B will be recreated when called later on.

How do you ensure that the user can return to the previous fragment by pressing the back button?

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.

How do I remove all fragments in the Backstack?

Explanation: MainFragment -> Fragment A -> Fragment B (this is added to backstack) -> Fragment C -> MainFragment (clear backstack ).


2 Answers

E.g. you can do following:

  • add fragA without adding it to backStack. So it always be in
    activity, and won't react on back button.
  • when you open fragD you should clear fragment BackStack. So when you press back button from D frag you go back to A.

P.S. There are other ways how to do what you want. It depends...

like image 127
Leonidos Avatar answered Sep 28 '22 15:09

Leonidos


Because the "back stack" has a stack-like behaviour...last-in, first-out...the last fragment you added to the back stack will be popped out of the back stack first. You will need to implement the behaviour you required manually by specifying your own. This is not that hard using the FragmentManager class methods.

If you "tag" your fragments as you add them to the transaction...

 fragmentTransaction.add(new FragmentA(), "FragmentA_Tag");

you can later determine which fragment to show when the back button is pressed...

FragmentA f = fragmentManager.findFragmentByTag("FragmentA_Tag");
if(f != null){
    f.show();
}

How you determine which fragment to show it's entirely up to you. You can keep track of the current visible fragment or you can use the isHidden method of the Fragment class...BTW, I'm talking about native fragments here, not support library's fragment.

like image 32
Leo Avatar answered Sep 28 '22 16:09

Leo