```NavHost(startDestination = "profile/{userId}") ...
# compose
m
Copy code
NavHost(startDestination = "profile/{userId}") {
    ...
    composable("profile/{userId}") {...}
}
Hello how to pass argument to navhost start destination
h
For a good UX, the start point should be static. But you could use "profile/42" as a start point, or get the userID from the user settings (or even from a server)...
m
hmm for me it's not working. navigation destination 1450140917 is not a direct child of this NavGraph
s
I think you need something like
startDestination = "profile/42"
. Replace 42 with your user id
m
Copy code
NavHost(
    navController,
    startDestination = "profile/1234"
) {
    composable("profile/{userId}", listOf(navArgument("userId") { type = NavType.StringType })){
        val test =
            it.arguments?.getString("userId").orEmpty()
        Timber.e("ProductListFeature" + " "+ test)
    }
}
i wrote some basic code, and i got the error navigation destination 1450140917 is not a direct child of this NavGraph
🤔 1
h
This should work 😅
Copy code
NavHost(
    navController,
    startDestination = "profile/1234"
) {
    composable("profile/{userId}") {
        val test = it.arguments?.getString("userId").orEmpty()
        Timber.e("ProductListFeature" + " "+ test)
    }
}
m
did you test? for me it's not working 😕
should we have multiple navhost or should we limit to one navhost ?
i
The string in the
startDestination
needs to match the string in the
route
exactly, you can't use that to provide default values for arguments. Instead, use the actual APIs for defining default values of arguments:
Copy code
composable("profile/{userId}",
  arguments = listOf(navArgument("userId") { defaultValue = "1234" }) {
}
m
but can i pass arguments into navHost startDestination ? or this is unavailable
i
That's what setting a default value does
Now, your start destination has a value for its arguments that you've set at graph construction time
m
great, i wil try 🙂
@Ian Lake thank you for your help 🙂
i
Generally the only valid use case for this type of approach is that you have activity extras that don't change that you're trying to pass down to your hierarchy. For just about everything else, there's a better structure that avoids this whole situation entirely, but we'd need to know more about what exactly you're trying to do to recommend the better alternative
m
@Ian Lake so what is the best approach when you try to build multiple module navigation? For now i use multiple NavHost and multiple navController? that's why i need this default value. Maybe i should use navigation() ?
for now i use multiple navhost, because i can have more seperated modules.
@Ian Lake only what i have found is https://kotlinlang.slack.com/archives/CJLTWPH7S/p1617813613356100?thread_ts=1617811540.352000&cid=CJLTWPH7S
NavGraphBuilder.moduleNameGraph(NavController)
but i have concern about it. How can i create shared view model
hiltViewModel
- i must do it in composable function so then i must create and pass this viewmodel as parameter of moduleNameGraph it's not looks nice
i
You absolutely shouldn't be using multiple NavControllers for that use case, no. Note that if you really have a ViewModel scoped to a navigation graph that is above multiple, entire modules and their subgraphs (which, if so, really make sure is the right thing for your use case), then you don't have to pass the whole shared ViewModel - just its route string. Then you're only
hiltViewModel(navController.getBackStackEntry(sharedGraphRoute)))
away from getting that ViewModel where you need it
m
@Ian Lake thank you again after your suggestion I limited navhost to two - one for main navigation and one for bottom bar
i
That's not how you should do that part either 😄 See our previous conversation: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1621449832286900?thread_ts=1621445818.275300&cid=CJLTWPH7S
m

https://c.tenor.com/3eIvVsG3yPYAAAAM/the-universe-tim-and-eric-mind-blown.gif

But again thank you :) @Ian Lake