I'm using the latest version of Jetpack Compose with type-safe arguments for navigation library. I have a nested navigation structure, and I need to pass a parameter to the start destination of a nested graph. But it seems giving me error
@Serializable
data object ScreenA
@Serializable
data class ScreenB(val id: String)
@Serializable
data object ScreenC
@Serializable
data object ScreenBAndCGraph
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
AndroidTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
val navController = rememberNavController()
NavHost(
navController = navController,
modifier = Modifier
.fillMaxSize()
.padding(innerPadding),
startDestination = ScreenA
) {
composable<ScreenA> {
Column {
Button(
onClick = {
navController.navigate(ScreenBAndCGraph)
}
) {
Text("Go to screen B")
}
}
}
navigation<ScreenBAndCGraph>(startDestination = ScreenB) {
composable<ScreenB> {
}
composable<ScreenC> {
}
}
}
}
}
}
}
}
Error on this line of code
navigation<ScreenBAndCGraph>(startDestination = ScreenB)
ERROR
StartDestination should not be a simple class name reference. Did you mean to call its constructor Companion(...)? If the class Companion does not contain arguments, you can also pass in its KClass reference Companion::class
How can I correctly pass the parameter to the start destination of the nested graph while ensuring type safety?
NOTE
I DON'T WON'T TO PASS DEFAULT ARGS VALUE
I'm using the latest version of Jetpack Compose with type-safe arguments for navigation library. I have a nested navigation structure, and I need to pass a parameter to the start destination of a nested graph. But it seems giving me error
@Serializable
data object ScreenA
@Serializable
data class ScreenB(val id: String)
@Serializable
data object ScreenC
@Serializable
data object ScreenBAndCGraph
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
AndroidTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
val navController = rememberNavController()
NavHost(
navController = navController,
modifier = Modifier
.fillMaxSize()
.padding(innerPadding),
startDestination = ScreenA
) {
composable<ScreenA> {
Column {
Button(
onClick = {
navController.navigate(ScreenBAndCGraph)
}
) {
Text("Go to screen B")
}
}
}
navigation<ScreenBAndCGraph>(startDestination = ScreenB) {
composable<ScreenB> {
}
composable<ScreenC> {
}
}
}
}
}
}
}
}
Error on this line of code
navigation<ScreenBAndCGraph>(startDestination = ScreenB)
ERROR
StartDestination should not be a simple class name reference. Did you mean to call its constructor Companion(...)? If the class Companion does not contain arguments, you can also pass in its KClass reference Companion::class
How can I correctly pass the parameter to the start destination of the nested graph while ensuring type safety?
NOTE
Share Improve this question asked Mar 19 at 15:17 Vivek ModiVivek Modi 7,46320 gold badges103 silver badges232 bronze badgesI DON'T WON'T TO PASS DEFAULT ARGS VALUE
2 Answers
Reset to default 1You can try passing the parameter to the NavGraph instead of the screen:
@Serializable
data object ScreenB
@Serializable
data object ScreenC
@Serializable
data class ScreenBAndCGraph(val id: String)
You have to pass the default args value.
navigation<ScreenBAndCGraph>(startDestination = ScreenB(id = 0))
navController.navigate(ScreenBAndCGraph) -> toRoute<ScreenB>().id == 0
navController.navigate(ScreenB("actual id")) -> toRoute<ScreenB>().id == "actual id"
You can add the same args name to data class ScreenBAndCGraph(val id: String)
navController.navigate(ScreenBAndCGraph("actual id")) -> toRoute<ScreenB>().id == "actual id"