Search code examples
android-jetpack-composeandroid-jetpackandroid-jetpack-navigation

How to change Jetpack compose navigation component backstack behaviour?


I implemented the login, sign-up with jetpack compose on an app. I redirected the logged-in users to the main screen and keep the new user on to the login screen. But the problem is when I redirect the user from the login screen to the main screen and the user press the back button the login screen appears. Now I know it's a normal behavior of the navigation component but I need to change it. How can I do that?

Used this line for navigating to the main screen from the login screen

navController().navigate("main_screen")


Solution

  • You can remove last item from the back stack before navigating to the main screen using popBackStack:

    navController.popBackStack()
    navController.navigate("main_screen")