The following code has 2 nav graphs and a shared nested route.
One of the nav graphs requires a user ID argument. The other does not. I can't navigate to the other one without supplying a user ID. Why is that and what is the best approach to take here.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NestedNavigationGraphsGuideTheme {
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = "foo"
) {
navigation(
route = "foo",
startDestination = "screen_1",
) {
composable("screen_1") {
Column {
Text(text = "SCREEN 1")
Button(
onClick = {
navController.navigate("bar?$userIdArg=9999")
}) {
Text(text = "GO TO BAR GRAPH (SCREEN 2)")
}
Button(
onClick = {
navController.navigateToEmployeePin(path = "from_screen_1")
}) {
Text(text = "GO TO EMPLOYEE PIN FROM FOO GRAPH")
}
}
}
composable(
route = EMPLOYEE_PIN_ROUTE,
arguments = listOf(
navArgument(pathArg) {
type = NavType.StringType
},
)
) {
// we can't load this screen, it's expecting a userIdArg which is
// completely irrelevant to this composable
Text(text = "EMPLOYEE PIN ROUTE, WITHIN FOO GRAPH")
}
}
navigation(
route = "bar?$userIdArg={$userIdArg}",
startDestination = "screen_2",
arguments = listOf(
navArgument(userIdArg) {
type = NavType.IntType
},
)
) {
composable("screen_2") {
Text(text = "SCREEN 2")
Button(
onClick = {
navController.navigateToEmployeePin(path = "from_screen_2")
}) {
Text(text = "GO TO EMPLOYEE PIN FROM BAR GRAPH")
}
}
composable(
route = EMPLOYEE_PIN_ROUTE,
arguments = listOf(
navArgument(pathArg) {
type = NavType.StringType
},
)
) {
Text(text = "EMPLOYEE PIN SCREEN (WITHIN BAR GRAPH)")
}
}
}
}
}
}
}
const val userIdArg = "userId"
const val pathArg = "path"
const val EMPLOYEE_PIN_ROUTE = "employee_pin_route/{$pathArg}"
fun NavController.navigateToEmployeePin(
navOptions: NavOptions? = null,
path: String,
) {
this.navigate(
EMPLOYEE_PIN_ROUTE.replace("{$pathArg}", path),
navOptions
)
}