

Going back partway through the stack isn’t particularly easy with this approach.

What if you add another “level” of back stack management into your app? Then you need to keep track of that too. When I used this approach, I ran into a few quirks that I didn’t like.įirst and foremost, it isn’t flexible.

Here is one way to handle popping off a certain subset of the backstack yourself by keeping track of how many “subscreens” are on the stack: One solution: manage the backstack yourself Before you do that, however, make sure you understand the framework and how it can work for you. This is true, and you shouldn’t be afraid to manage the back stack yourself if the framework simply isn’t going to work in your scenario. You can manage the back navigation of your application however you want, with or without the framework. In Advocating Against Android Fragments, Pierre-Yves Ricau teaches an important lesson- there’s nothing particularly magical about the Android framework’s back stack. If you want to jump right to the sample code, you can find it here on GitHub. If we start on Tab 1, drill down to a sub screen, then switch to Tab 2, we may want to pop off all the subscreens on Tab 1 before switching to the second tab. Take, for example, an app that uses bottom navigation. However at some point you are likely going to run into a situation that doesn’t quite fit the mold of a an application with a simple back stack. The Android framework provides simple APIs for managing your back stack without headache for most simple applications. Or: A lesson in not fighting the framework
