最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Unable to pass args to nested start destination in jetpack compose - Stack Overflow

programmeradmin2浏览0评论

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

I DON'T WON'T TO PASS DEFAULT ARGS VALUE

Share Improve this question asked Mar 19 at 15:17 Vivek ModiVivek Modi 7,46320 gold badges103 silver badges232 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You 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"
发布评论

评论列表(0)

  1. 暂无评论