Search code examples
androidnavigationargumentsandroid-jetpack-compose

Compose navigation argument string includes curly braces?


I have a composable in my NavigationHost class that I use to navigate to a NotesScreen() looking like this:

composable(
        route = NotesScreen.route + "/{$USER_ID_KEY}"
    ) { backStackEntry ->
        NotesScreen(
            navController = navController,
            userId = backStackEntry.arguments?.getString(USER_ID_KEY)
        )
    }

The issue is when I read the userId string inside the NotesScreen() function, it still includes the curly braces as part of the passed argument string, like "{NIsKtJ4QqteqSEwuMI7}". How can I make sure I get a clean string without the curly braces?


Solution

  • When you call

    navController.navigate(NotesScreen.route + "/{$userId}")
    

    Your final string looks something like

    notes/{NIsKtJ4QqteqSEwuMI7}
    

    It seems your code is purposefully adding an extra { and }, which then get matched to your pattern of

    notes/{userId}
    

    e.g., everything after the NotesScreen.route is used as the userId - the curly braces indicate the location of the placeholder for your userId argument.

    So if you don't want to add extra braces to your userId, don't add them when you call navigate:

    navController.navigate(NotesScreen.route + "/$userId")