问题
I want to remove a fragment from backstack and put another fragment in its place which hasn't been created yet. Below is a picture of what I want to achieve.
Is there any way to accomplish this?
EDIT:
As pointed out by @Elltz, it's not possible. So my question becomes
Is it possible to destroy the last fragment from back stack?
Can I handle the back button pressed manually to provide a different fragment than what is on the back stack?
回答1:
NO! NO! NO !
want to remove a fragment from backstack and put another fragment
you can not manually re-arrange the BackstackEntry. you can only listen,observe and call it. look into PopBackstack(String name, int flags) hence the first no
and put another fragment in its place which hasn't been created yet
you mean a null Fragment ?.. you can not also assign references to null object, the second no
Is there any way to accomplish this?
A confirmation No.
EDIT
YES! YES! YES!
Is it possible to destroy the last fragment from back stack?
you can; using FragmentManager & FragmentTransaction
FragmentManager fm = FragmentActivity.getSupportFragmentManager();
fm.beginTransaction().remove(
fm.getBackStackEntryAt(fm.getBackStackEntryCount()-1););
the first yes
Can I handle the back button pressed manually to provide a different fragment than what is on the back stack?
Yes, you can, but you have to do it in your FragmentActivity or Activity so you will need some interface or your own logic around that, or
with your own logic implementing BackStackChangedListener, this is actually the interface i was talking about with logic, this listener gets notified anytime a Fragment goes or leaves the Backstack, hence the second yes
Third yes was for pampering.
Hope it helps Sir.
回答2:
You cannot actually do this, but what you can do is, make these tansitions but hidden
go to fragment 3 from fragment 2
go to fragment 4 from 3.
By hidden I mean, that user does not see this happening (you can set the layout visibility as gone).
But this way, Fragment 2 will be in the stack.
To take out fragment 2 from the stack, do the following hidden operations:
go back to 1
go to 3 from 1
go to 4 from 3.
Hope this helps.
来源:https://stackoverflow.com/questions/30416815/can-i-put-a-fragment-on-backstack-which-hasnt-been-initialized-yet