Using a Navigation Compose deep link, is it possib...
# compose
c
Using a Navigation Compose deep link, is it possible to pop destinations off the synthetic back stack, as if we navigated to the deep link's destination using popUpTo inclusive?
Use case: My nav graph's start destination is just a splash screen that reads some saved data to determine what the actual home screen should be (e.g. "login" or "welcome"), which it then navigates to, with popUpTo inclusive set to the root nav graph, so that the user can't navigate back to the splash screen. But if a user enters a deep link to the "welcome" screen, the deep link will automatically add the splash screen to the back stack because it is the start destination. I don't want this, I want there to be nothing on the back stack so that pressing back will exit the app instead of showing the splash screen (which then automatically navigates to the "welcome" screen, leading to an infinite loop).
a
Because you’re working against the navigation principles. Splash shouldn’t be your start destination https://twitter.com/ianhlake/status/1240382348668964864
☝️ 1
i
Yep, you've discovered one of the many reasons why that should be your start destination (it breaks a lot of other stuff too). There's a number of previous threads on both login and splash screens in this channel
c
Thank you, makes sense!
@Ian Lake I tried implementing the suggested approach in the Conditional Navigation documentation, however my use case is slightly different, so an issue arose. My app has no
main_fragment
equivalent which can be a start destination that does not require auth. There is a login screen and a welcome screen, but the welcome screen requires auth. So it seems like to follow the principles of navigation, the welcome screen should be the start destination. Following the Conditional Navigation example, if the welcome screen opens and we have no auth, we redirect to the login screen. But if the user then presses the system back button on the login screen without logging in, we will return to the welcome screen which sees that login was unsuccessful via the saved state handle. From that point, in the example, we navigate to the unauthenticated start destination,
main_fragment
. But in my app there is no unauthenticated start destination we can navigate to. We are already at the start destination (welcome screen), which requires auth. Ideally we should just exit the app at this point (if the user presses back from the login screen), but I see no way to do this via the nav controller. Calling
popBackStack
from the welcome screen, even if we explicitly pop up to the start destination or the nav graph itself (inclusive), does not exit the app but just appears to display the nav graph on its own with no destination inside of it. I could instead invoke a callback to the activity's
onBackPressed
or
finish
method, but is this really the ideal way? This seems like a common use case and I feel like I should be able to handle it properly from within the Navigation Component.
i
You would need to call
finish()
on the activity if you want to close the activity; NavController will never finish your activity for you; it's back stack and state is completely contained within the activity
c
Okay, in that case I think the simplest solution would be to add a
BackHandler
to the composition on the login screen, which invokes a callback to the activity's
finish
method. It seems that using the saved state handle is not necessary for my use case. I appreciate the help.