https://kotlinlang.org logo
#compose
Title
# compose
j

Jason Ankers

02/24/2021, 2:54 PM
I’m using a nested nav graph called “authenticated” to represent all the authenticated routes of my app. I want launch some tasks (fetch data, connect to a bluetooth device) when the user navigates to any route within this nested graph. Since
NavGraphBuilder.navigation
is an extension function and not a composable, I can’t see any way call
LaunchedEffect
from a nested graph. Do I need to use nested NavHost’s here instead?
p

ppvi

02/24/2021, 2:59 PM
You would have to use LaunchedEffect from inside each
composable { }
. However doing so will probably crash your app due to https://issuetracker.google.com/177825470
i

Ian Lake

02/24/2021, 3:02 PM
Note that an authenticated graph isn't a good strategy, particularly when you consider deep links and restoring your state and process death - every destination that requires auth needs to check if the auth is still valid and redirect to your login destination if it isn't as there's no guarantee your previous login destination / start destination of your authenticated graph has actually ran since your last process creation
j

Jason Ankers

02/24/2021, 3:32 PM
Yeah fair point - but I feel like it would be easier to just add an auth check when the app is foregrounded instead of having to manually check every authenticated destination. Most large apps have a huge number of authenticated routes, it would be very convenient to launch some business logic at the graph level
i

Ian Lake

02/24/2021, 3:36 PM
Sounds like an
AuthCheck(navController)
Composable would encapsulate your business logic quite well
2
j

Jason Ankers

02/24/2021, 3:58 PM
Also without an authenticated nav graph, how could I scope viewmodels, etc to the authenticated region of my app? For example I scope the lifetime of a bluetooth device to the authenticated graph
i

Ian Lake

02/24/2021, 5:51 PM
Auth scoped objects is well worn territory in the dagger world, I don't know why you'd try to force being in a particular nested graph into that
c

Colton Idle

02/24/2021, 8:44 PM
Ooh. I've got to look into this for dagger. I've been thinking about auth scoped objects for a while and using dagger for it always seemed to make sense
j

Jason Ankers

02/25/2021, 2:02 AM
I also didn’t know about auth scoped objects with Dagger. Thanks @Ian Lake