

To ensure that the saved instance state is saved when calling saveBackStack(), we need to also inject a call to onSaveInstanceState() at the right point in the fragment lifecycle transitions. While fragments have always saved the Fragment’s view state, the only time that a fragment’s onSaveInstanceState() would be called would be when the Activity’s onSaveInstanceState() was called. Paying down our technical debts in Fragments This didn’t come without paying down a lot of technical debt though. This is how the restoreBackStack() API can later recreate those transactions and their fragments from the saved state and effectively ‘redo’ everything that was saved. saveBackStack() does the same reversal that popping the transaction does, but it ensures that the view state, saved instance state, and ViewModel instances are all saved from destruction. This is the main difference between that API and the new saveBackStack(). This means you lose your view state, any saved instance state, and any ViewModel instances you’ve attached to that fragment are cleared. This means that popBackStack() is a destructive operation: any added fragment will have its state destroyed when that transaction is popped. Note: I cannot stress this enough, but you absolutely should never interleave transactions with addToBackStack() and transactions without in the same FragmentManager: transactions on your back stack are blissfully unaware of non-back stack changing fragment transactions - swapping things out from underneath those transactions makes that reversal when you pop a much more dicey proposition. This puts the FragmentManager back into the same state that it was before the fragment transaction was initially committed. When you call popBackStack() (either directly or via FragmentManager’s integration with the system back button), the topmost transaction on the fragment back stack is reversed - an added fragment is removed, a hidden fragment is shown, etc. FragmentManager then holds onto that transaction as part of its back stack. This means when you commit() a fragment transaction with addToBackStack(), the FragmentManager is going to execute the transaction by going through and executing each of the operations (the replace, etc.) that you specified on the transaction, thus moving each fragment through to its expected state. Specifically, the ones that have used the addToBackStack(String name) API.

The FragmentManager’s back stack isn’t made up of fragments, but instead is made up of fragment transactions.

Multiple back stacks in FragmentsĪt the surface level, the support for multiple back stacks is deceptively straightforward, but requires a bit of an explanation of what actually is the ‘fragment back stack’. This has a profound effect on how the multiple back stack APIs work. The system back button is still a one directional command - ‘go back’. Multiple back stacks doesn’t change these fundamentals. That means when you use either Fragments or Navigation, they use the OnBackPressedDispatcher to ensure that if you’re using their back stack APIs, the system back button works to reverse each of the screens that you’ve pushed onto the back stack. This is actually the same API that FragmentManager and NavController already plug into. Instead, there are APIs for custom back navigation in the OnBackPressedDispatcher. While in the past you might have been tempted to override the onBackPressed() method of your activity to customize this behavior, it is 2021 and that is totally unnecessary. In the simplest cases, the system back button just finishes your activity.
#Stack the states 2 apk android
Whether you’re using Android’s new gesture navigation system or the traditional navigation bar, the ability for users to go ‘back’ is a key part to the user experience on Android and doing that right is an important part to making your app feel like a natural part of the ecosystem. If a ‘back stack’ is a set of screens that you can navigate back through via the system back button, ‘multiple back stacks’ is just a bunch of those, right? Well, that’s exactly what we’ve done with the multiple back stack support added in Navigation 2.4.0-alpha01 and Fragment 1.4.0-alpha01! The joys of the system back button

A deep dive into what actually went into this feature
