Search code examples
androidandroid-fragmentstransactionsstackback-stack

Android Fragments Backstack: Is it a stack of Fragments or transactions?


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 fragments?

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?


Solution

  • 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 Fragments, 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.