[compose-navigation] Given an authentication flow ...
# compose
a
[compose-navigation] Given an authentication flow in an app, which can go deeper with sign ups, mfa and stuff, how should I go about on going back to the auth-required screen with compose? I was looking to use something like
navController.popBackstack(route="authParent")
but there’s no API like that. I can’t point to a specific route from the navigation flow because the app can navigate to Auth from different screens.
Thinking of something like this. Or is there a better way of “finishing” a flow with compose-navigation
i
You can pop the whole graph by using the route of the graph with
popBackStack()
a
🤔 I’m in compose beta05 and compose-navigation alpha10 but I can’t find a
popBackStack(route)
Or do you mean something like
Copy code
navController.navigate("auth_nav") {
    popUpTo("auth_nav") { inclusive = true }
}
?
i
That would pop the whole auth_nav, then navigate to a new copy of it, which isn't what you want
🤦 1
a
Right
trying to find this api from the source… sorry if I missed it. There is a
popBackstack(id)
but the id is implementation detail in compose-nav
j
This is probably a hacky solution, but I used nested navigation graphs. The reason why I used it is that the google docs said a potential use of nested navigation is "self contained login flow." Still feels kinda hacky...
i
Yeah, looks like we're missing that overload. https://issuetracker.google.com/issues/172823546 tracks bringing routes entirely up to par with IDs in every API surface
👍 1
a
Yeah my auth flow is a nested navigation. Do you have a way to pop the whole nested graph currently? @Justin Yue
i
Yes, you would want your login flow to be its own graph (and never the start destination of your whole app, as per the Principles of Navigation)
👍 2
j
So you know how you have 2 navigation host controllers, right? I had to pass in the one that was nesting (let's call this host A) into the nested graph (host B), so if I wanted to go back to the login within host B, i would call host A and navigate back to the login route. Again, feels kinda hacky, and I'm open to improvements here
a
Yes, I try to follow principles as much as I can 🙂 Thanks! Will this update get pushed it on the next version? Otherwise I would need to think of some workaround for now.
i
You shouldn't and don't need to use separate NavHosts for this, no
a
(actually I have already tried copying the internal
createRoute(route).hashCode
to make use of other APIs but for another use case 😆)
i
What I'd suggest is using
navController.getBackStackEntry("auth_nav").destination.id
And pass that to
popBackStack()
a
Oh 🤦 I haven’t thought of that.
j
Interesting, I will try that!
a
This should be good enough for now, thanks!
@Justin Yue I’ve tried working with multiple navhosts in another thing and it just makes the navigation behavior questionable in a lot of things 😅.
j
Yah definitely. My solution just happened to work and I was like, aight 😅