Android Compose NavHost popback to one of multiple instances of same destination chosen by route argument

32 Views Asked by At

I have a NavHost which contains a route with a single argument (like "name/{argument}") and navigate to this route multiple times in a row, each time with a different argument. NavHost also contains a summary page, which should allow to go back to selected instance of aforementioned route (clearing the navigation stack up to this point), distinguished by argument that was passed to it. Unfortunately, when I use NavigationController.popBackStack("route/argumentValue") it seems to ignore the argument value and goes back to topmost instance of this route instead.

How could I achieve the intended behavior ?

Sample code:

@Composable
fun Test() {
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = "startPage") {
        composable("startPage") {
            Column {
                Text("startPage")
                Button(onClick = {
                    navController.navigate("questionPage/aaa")
                    navController.navigate("questionPage/bbb")
                    navController.navigate("questionPage/ccc")
                    navController.navigate("questionPage/ddd")
                    navController.navigate("review")
                }) {
                    Text("test")
                }
            }
        }
        composable("questionPage/{questionId}") {
            val arg = it.arguments?.getString("questionId") ?: "---"
            Text("question: $arg")
        }
        composable("review") {
            Column {
                Text("review")
                Button(onClick = {
                    // opens page with "ddd" instead
                    navController.popBackStack("questionPage/bbb", false)
                }) {
                    Text("go back to bbb")
                }        
            }
        }
    }
}
0

There are 0 best solutions below