admin管理员组文章数量:1406177
this is my nav graph
NavHost(navController, startDestination = Splash) {
composable<Splash> { SplashScreen(navController) }
navigation<Home>(startDestination = Editor) {
composable<Editor> { navBackStackEntry->
val data= navBackStackEntry.toRoute<Home>() // right way crash app
val path = navBackStackEntry.arguments?.getString("path") // workaround works fine
....
}
serialized objects:
@Serializable
object Splash
@Serializable
data class Home(val path: String)
@Serializable
object Editor
Problem:
When navigating from the Splash screen to Home using navController.navigate(route = Home("my path")), I am unable to access the path argument in the Editor composable like this: 'navBackStackEntry.toRoute()'.
After some research, I realized that the issue arises because Editor is not the destination of the navigation in the first instance (it's Home that is), so it's not possible to directly retrieve arguments from Editor even though the path argument is available in the bundle. While I can access it via navBackStackEntry.arguments?.getString("path"), I am wondering if there is a more robust solution.
this is my nav graph
NavHost(navController, startDestination = Splash) {
composable<Splash> { SplashScreen(navController) }
navigation<Home>(startDestination = Editor) {
composable<Editor> { navBackStackEntry->
val data= navBackStackEntry.toRoute<Home>() // right way crash app
val path = navBackStackEntry.arguments?.getString("path") // workaround works fine
....
}
serialized objects:
@Serializable
object Splash
@Serializable
data class Home(val path: String)
@Serializable
object Editor
Problem:
When navigating from the Splash screen to Home using navController.navigate(route = Home("my path")), I am unable to access the path argument in the Editor composable like this: 'navBackStackEntry.toRoute()'.
After some research, I realized that the issue arises because Editor is not the destination of the navigation in the first instance (it's Home that is), so it's not possible to directly retrieve arguments from Editor even though the path argument is available in the bundle. While I can access it via navBackStackEntry.arguments?.getString("path"), I am wondering if there is a more robust solution.
Share Improve this question asked Mar 6 at 13:59 XenioneXenione 2,2131 gold badge25 silver badges31 bronze badges1 Answer
Reset to default 0You're correct that toRoute<Home>()
crashes because Editor
is not directly receiving the path
argument—it's Home
that gets it. Since Editor is inside a nested navigation graph under Home
, it does not automatically inherit the arguments.
Answer Editted through @xenione comment.
this might be your solution:
Define the nav graph:
NavHost(navController, startDestination = Splash) {
composable<Splash> { SplashScreen(navController) }
// Define Home with a path argument
navigation<Home>(startDestination = Editor) {
// Home gets the path argument
composable<Home> { navBackStackEntry ->
val homeData = navBackStackEntry.toRoute<Home>() // Extract the argument safely
val path = homeData.path
HomeScreen(navController, path)
}
// Editor now explicitly receives "path" as an argument
composable<Editor>(arguments = listOf(navArgument("path") { type = NavType.StringType })) { navBackStackEntry ->
val path = navBackStackEntry.arguments?.getString("path") ?: ""
EditorScreen(navController, path) // Safe and type-safe
}
}
}
navigate like this:
navController.navigate(Home("my_path"))
Then, when navigating from Home → Editor
, manually include path
:
navController.navigate("editor?path=${URLEncoder.encode(path, "UTF-8")}")
let me know if it works.
本文标签: androidandroidxnavigation with composepass arguments thru nested graphs with typeSafeStack Overflow
版权声明:本文标题:android - androidx.navigation with compose : pass arguments thru nested graphs with typeSafe - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744971309a2635251.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论