https://kotlinlang.org logo
Title
m

Matti MK

07/05/2022, 6:17 PM
Is it possible to pass an argument when navigating to a separate NavGraph route? I’d ideally like to have something as follows:
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:
navigation(
        route = "routeToNavigateTo/{argument}",
        startDestination = "startDestination"
    ) {
        composable(route = "startDestination" ... ) { 
           // get arg from navBackStackEntry here
        }
    }
i

Ian Lake

07/05/2022, 8:11 PM
You can directly navigate to your
startDestination/{argument}
destination, you never need to go through the graph
👍 1
m

Matti MK

07/06/2022, 4:49 AM
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

Lasse Magnussen

02/01/2023, 12:39 PM
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…