android-jetpack-composeandroid-jetpack-navigation

Disable back button in Jetpack Compose


How can I disable the back button in Jetpack Compose? I don't want the user to go back to previous screen.

I tried below code but the user can still go back to the previous screen:

BackHandler(enabled = false) {
   // do nothing
}

Solution

  • You should set enabled to true for taking control of back button. Then call BackHandler from the current destination in your NavHost

    NavHost(
        navController = navController,
        startDestination = startDestination
    ) {
    
        composable(
            route = "Your Destination Route"
        ) {
    
            BackHandler(true) {
                // Or do nothing
                Log.i("LOG_TAG", "Clicked back")
            }
    
            YourDestinationScreen()
        }
    }