Is it possible to pass an argument when navigating...
# compose
m
Is it possible to pass an argument when navigating to a separate NavGraph route? I’d ideally like to have something as follows:
Copy code
navigation(
        route = "routeToNavigateTo",
        startDestination = "startDestination/{argument}"
    ) {
        composable(route = "startDestination/{argument}" ... ) { ... }
    }
Apparently yes. Only route needs to have an argument provided, it’ll then be passed on to the
startDestination
. Something like:
Copy code
navigation(
        route = "routeToNavigateTo/{argument}",
        startDestination = "startDestination"
    ) {
        composable(route = "startDestination" ... ) { 
           // get arg from navBackStackEntry here
        }
    }
i
You can directly navigate to your
startDestination/{argument}
destination, you never need to go through the graph
👍 1
m
Well, that explains the lack of SO answers for the question 😅 For some reason I found it logical to navigate to a graph and then let the existing graph definition (
navigation(...)
) handle the
startDestination
.
l
Blast from the past. I’m running into a similar problem now. It’s basically the same, but the only difference MIGTH be that it’s a nav graph inside an Activity. So I use
createGraph
to create the graph, but my first destination wants data from the intent. The graph is also reused elsewhere, nested in a different activity. And I must admit; I don’t fully grasp what “directly navigate to your
startDestination/{argument}
means — It makes sense to me for a nested nav graph where the entire graph starts off with some other start destination. Trying to convert some XML nav graph to Kotlin…
I figured out the missing piece of the puzzle for me. I was setting up the nav graph by assigning straight to
NavController.graph
- while this works there’s no way to pass in the
startDestinationArgs
— these can, however, be supplied in
NavController.setGraph
I simply passed along
intent.extras
and everything worked like a charm…