Thread
#compose
    Jason Ankers

    Jason Ankers

    1 year ago
    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?
    ppvi

    ppvi

    1 year ago
    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

    1 year ago
    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
    Jason Ankers

    Jason Ankers

    1 year ago
    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

    1 year ago
    Sounds like an
    AuthCheck(navController)
    Composable would encapsulate your business logic quite well
    Jason Ankers

    Jason Ankers

    1 year ago
    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

    1 year ago
    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
    Colton Idle

    Colton Idle

    1 year ago
    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
    Jason Ankers

    Jason Ankers

    1 year ago
    I also didn’t know about auth scoped objects with Dagger. Thanks @Ian Lake