0

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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

navigation<Home>(startDestination = Editor) { /*lambda*/ } is of type NavGraphBuilder.() -> Unit, so you don't have access to navBackStackEntry. The lambda is only used to define the navigation graph.
correct, I guess I missed that @Xenione I updated the answer, this might work now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.