I'm getting an error when trying to navigate to an...
# compose
a
I'm getting an error when trying to navigate to another graph with arguments. I'm not sure what I'm doing wrong here This is the error I'm getting:
java.lang.IllegalArgumentException: Wrong argument type for 'userId' in argument bundle. integer expected.
code:
Copy code
object ProfileDestination : GenericDestination {
    override val route = "profile_route/{userId}"
    override val startDestination = "profile_destination/{userId}"
    const val argsUserId = "userId"
    operator fun invoke(id: Int) = "profile_route/$id"
}

fun NavGraphBuilder.profileGraph() {
    navigation(startDestination = ProfileDestination.startDestination, ProfileDestination.route) {
        composable(
            route = ProfileDestination.startDestination,
            arguments = listOf(navArgument(ProfileDestination.argsUserId) {
                type = NavType.StringType
            })
        ) { Profile() }
    }
}
Copy code
navController.navigate(ProfileDestination(1))
Changing the NavType to StringType solves the issue for some reason. I'm not sure why it doesn't accept integers
i
It looks like you are navigating to the navigation graph (the
navigation
element), which doesn't have any
arguments
defined, so you're just getting the default, which is strings. Generally, you'll want to navigate directly to the destination rather than to a navigation graph:
Copy code
operator fun invoke(id: Int) = "profile_destination/$id"
tbh, I don't know why you have the navigation graph there at all. your whole
profileGraph
could just be a single
composable
destination, skipping the intermediate layer entirely?
a
I'm splitting features of the app to have their individual navigation graph. Basically the same way that it was recommended in the fragment navigation when having a multi-module app. Is this not the case with navigation compose anymore?
What are the uses of the nested graphs, I'm guessing it can be used to define different starting destinations based on the business logic. Other than that should I just navigate to composables directly within the same graph?
i
Having separate navigation graphs is great, do that - it'll affect your generated back stack when you deep link to a destination and give you a shared scope for all destinations within that graph. There's just no reason to navigate to the graph itself instead of directly to the destination you want to go to
a
Thanks! That was really helpful