Search code examples
androidkotlinandroid-jetpack-composekotlin-coroutinesandroid-jetpack-navigation

How to handle mutiple clicks on popBackStack() in jetpack compose


When i click on back icon multiple times it calls navController.popBackStack() that many times and leaves me with a blank screen. How to deal with it?

 Scaffold(
            topBar = {
                PhotoDetailTopBar(
                    onBackClick = {
                        navController.popBackStack()
                    },
                    onInfoClick = { isSheetOpen = true }
                )
            }
        )

I want the navController.popBackStack() to sleep/disable until it finishes


Solution

  • You get blank screen because there are no more destinations on the back stack to be popped.

    Method popBackStack() returns false if back stack was empty and nothing was popped. For more details look at the documentation here.

    You might want to do something like this

    if(!navController.popBackStack()){
       //navigate to starting screen of your app
       navController.navigate("route_of_starting_screen")
    }