PopBackStack but keep the first fragment in androi

2020-02-05 12:25发布

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

8条回答
淡お忘
2楼-- · 2020-02-05 13:24

Mark my words, complete FragmentTransactions are added to backstack not just a fragment, means even if you add and remove fragment in a single transaction calling poBackStack() will reverse the complete transaction. Passing a tag in its argument pops all the transaction till the tagged transaction or even pops the tagged transaction (in case FragmentManager.POP_BACK_STACK_INCLUSIVE added to the argument) So it's more about how you added it not how you removing it. See Performing Fragment Transactions

查看更多
叼着烟拽天下
3楼-- · 2020-02-05 13:26

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

查看更多
登录 后发表回答