When navigating manually to a nested navigation co...
# compose
h
When navigating manually to a nested navigation composable and then triggering a deeplink to a sibling composable in this navigation that is also already in the back stack we are getting an
Attempt to invoke virtual method 'androidx.navigation.NavDestination$DeepLinkMatch androidx.navigation.NavGraph.matchDeepLink(androidx.navigation.NavDeepLinkRequest)' on a null object reference"
exception. Anyone experiencing the same issue?
i
How are you 'then triggering a deeplink'? It sounds like you are calling
navigate()
as part of composition
h
The deeplink is triggered from a hyperlink inside an email. It's part of a sign-up flow. So the user puts the app to the background while on a screen in a nested navigation and then should be navigated to a screen in another (sibling) nested navigation when she/he clicks the link. It only works when we return to the root level of the navigation graph beforehand.
i
It sounds like you are doing a lot more work than you need to, considering that Navigation already does all of the work necessary for handling deep links (you shouldn't be calling navigate at all): https://developer.android.com/guide/navigation/navigation-deep-link
That's why there is a
deepLinks
parameter on
composable
h
Don't get me wrong - we are actually using the deepLinks parameter. The user navigates to some screen (using navigate()), then has to switch to the email client, click a hyperlink that is a deeplink defined on a navigation composable like explained in the documentation. We are in no way manually handling the deeplink.
i
Then I don't think that exception should be possible - the only way you'd get that exception is if you're calling navigate before the graph is actually set on the NavController (i.e., as part of that initial composition). NavController only handles external deep links after the graph is set. Do you mind sharing the entire stack trace?
h
Sorry, you were indeed right. We built the navigation like Joe Birch does in his Minimise app. Collecting “navigation commands” as state in the composable that contains the NavHost. See Joe’s solution here: https://github.com/hitherejoe/minimise/blob/main/platform_android/app/src/main/java/co/joebirch/minimise/android/MainActivity.kt Upon triggering the deeplink, the navigation command is collected again, causing the navigate() call.
i
Yeah, you really don't want to be treating events (navigating to a new destination) as state (something you act on for every recomposition) -
collectAsState()
is the wrong approach. You'd instead want a
LaunchedEffect
that calls
collect
on the Flow.
👍 2
f
Copy code
LaunchedEffect(navigationManager.commands){
    navigationManager.commands.collect{ command ->
        if (command.destination.isNotEmpty()) {
            navController.navigate(command.destination)
        }
    }
}