Android Navigation Components - How to pop startDestination off the back stack?

Viewed 1557

I have a single activity app that uses the Android Navigation Component for navigation. When a user launches my app, I want to show a Welcome Fragment that lets them choose how they'd like to work today (They can choose workflow A or workflow B).

Once the user chooses one of the options they are navigated to the Do Work Fragment that allows them to start working. If they click the back button at this point, I want the app to close, not to navigate back to the Welcome Fragment (The Welcome Fragment only shows on app startup).

The way I set my navigation graph up was to set the Welcome Fragment as the start destination and the Do Work Fragment as another destination on the same graph.

When the app is launched my Welcome Fragment is shown as desired, however, there doesn't seem to be a slick way to launch the Do Work Fragment from the Welcome Fragment AND pop the WelcomeFragment off the back stack so that it can't be navigated to again without actually killing and restarting the app.

What I've settled on is, from the Welcome Fragment, calling findNavController.popBackStack() and then findNavController().navigate(R.id.do_work_fragment). This seems to have the desired behavior, however it seems really weird to have to "manually" pop the back stack. At first I assumed this would never work since I figured popping the back stack with a single item on it would cause the app to close, but it appears to work just fine.

Am I overthinking this and my solution is just fine or is there a better way to solve this navigation scenario with Android's Navigation Components?

0 Answers
Related