popBackStackImmediate from 4th fragment to the 2nd

2019-03-17 01:28发布

问题:

I'm On the 4th fragment. On 4th fragment view when i click on cancel button it goes on 2nd fragment. but i use popBackStackImmediate it goes on 3rd fragment.

回答1:

fmgr.popBackStackImmediate(2, 0);

or

fmgr.popBackStackImmediate(3, FragmentManager.POP_BACK_STACK_INCLUSIVE);

should do the trick


You can also name your fragment in your transaction

fmgr.beginTransaction()
    .add(R.id.container, FRAGMENT, "your_fragment")
    .addToBackStack("your_fragment")
    .commitAllowingStateLoss();

fmgr.executePendingTransactions();

And then pop to this fragment:

fmgr.popBackStackImmediate("your_fragment", 0);