简体   繁体   中英

Jetpack Compose navigation NavController.popBackStack() not working properly

When working with Compose Navigation and calling NavController.popBackStack() multiple times on the first shown Composable ( startDestination ) the backnavigation does not work anymore. For example when navigating to another Composable from this point on and then calling popBackStack does not have an effect.

For some Reason the size of the NavController.backQueue is at least 2 even though it's supposed to only show one Composable. If popping the backstack lower than that, the navigation does not seem to work anymore. (I don't know why)

Therefore I wrote the following simple extension function which prevents popping the BackQueue lower than 2:

fun NavController.navigateBack() {
    if (backQueue.size > 2) {
        popBackStack()
    }
}

You can use it like this:

val navController = rememberNavController()
...
navController.navigateBack()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM