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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

You'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