Reputation: 1214
I have 6 buttons and one container. When I pressed a button, and other button, this add in backstack perfectly, but when I pressed a button that I had called before, this add too. So.. if already add back stack, how remove the old fragment when replace with the new fragment? Thanks. It's same logic that Instagram App.
For example:
Fragment A,B,C,D,E,F are my 6 buttons.
Pressed A,B,D,F when pressed back, F,D,B,A... Fine. But... Pressed A,B,C,B,C,B,F and presed back then --> F,B,C,B,C,B,A when should be F,B,C,A directly. Thanks. Sorry for my english.
Upvotes: 0
Views: 255
Reputation: 394
UPDATE:
For Fragment
s, I do not think there is a built-in mechanism in the back stack that will achieve your requirement.
However, you can keep track of things on your own and decide which of your Fragments to show when user hits the back key. The idea is to override the Activity
's onBackPressed() method and just use FragmentTransaction.replace without adding anything to the back stack ever.
As a side note, if you can use Activity
instead of Fragment
, then you can just use FLAG_ACTIVITY_REORDER_TO_FRONT to launch each Activity
, and it will behave exactly like you desired.
Original answer:
If you look at addToBackStack (String name), you can see that you can use a String to distinguish individual FragmentTransaction
s.
You can then later pass in a String and call popBackStack (String name, int flags) to restore the back stack to your desired state.
Upvotes: 1