PopBackStack but keep the first fragment in android

user782104 picture user782104 · Feb 6, 2015 · Viewed 22.8k times · Source

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

Answer

Leonidos picture Leonidos · Feb 6, 2015

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...