Reputation: 9020
From the developers guide,
When you perform such a fragment transaction, you can also add it to a back stack that's managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button.
Is this Backstack
a stack
of fragments
or some sort of transaction
records?
Because if it is a stack
of fragments
, and a fragment
was removed (which mean popping out of the backstack
I suppose), how would a back-navigation add back the fragment which was popped out of the stack
?
Then later in the same document,
Pop fragments off the back stack, with popBackStack() ...
This gives the idea that it is a stack
of fragment
s?
So what does a Fragment
's backstack
stack
contain, fragments or transaction records?
If the former, then how is a remove transaction reversed on back-navigation?
Upvotes: 1
Views: 335
Reputation: 9020
Is this Backstack a stack of fragments or some sort of transaction records?
A stack
of fragments.
If the former, then how is a remove transaction reversed on back-navigation?
In Fragment
s, you can add the fragments to a BackStack
(it's not automatically added to the BackStack
like it happens in the case of an activity
) - you have the option of doing it before removing the fragment
.
A Fragment
is added to the BackStack
only if you explicitly request that by calling addToBackStack()
during a transaction which removes the fragment.
So that way, it will be added to the backstack, and when the user navigates backwards, the last added fragment comes on the top of the backstack.
Upvotes: 3
Reputation: 8338
I think you may just be reading into this too much.
Yes, the backstack
is a stack
of fragments. When you start a new activity or commit a fragment transaction, the one you're leaving will by default be added to the stack. In this way, you create a chronological list of the fragments that you've gone through.
What the quote you provided means when it says "each back stack entry in the activity is a record of the fragment transaction that occurred" is that each new fragment was added on a fragment transaction, so you have a stack of previous fragments that is, in a way, a record of what you've gone though.
On back-navigation, a fragment is just popped off of the stack and put back on the screen, in effect letting you move backwards through where you came from.
Upvotes: 2