Let's think about it this way for a second
`NavController` is the object that controls screen transitions (navigate to, go back) — `NavHost` is the container that maps each route (a screen name string) to its composable function. Calling `navController.navigate("home")` switches to the composable for the 'home' route — calling `popBackStack()` takes you back to the previous screen, just like tapping the back button.
Let's connect this to a real scenario
If you set things up like `NavHost(navController, startDestination = "login") { composable("login") { LoginScreen(onLoginSuccess = { navController.navigate("home") }) }; composable("home") { HomeScreen() } }` — clicking the login button calls `navController.navigate("home")`, taking you to HomeScreen.
Let's walk through it together
@Composable
fun AppNavigation() {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = "login") {
composable("login") {
LoginScreen(onLoginSuccess = {
navController.navigate("home")
})
}
composable("home") {
HomeScreen(onLogout = {
navController.popBackStack()
})
}
}
}After logging in on the Login screen, you should see it automatically switch over to the Home screen.5-minute try-it
Run `AppNavigation` and actually try the LoginScreen → HomeScreen transition on the emulator — then press the physical device's Back button and confirm it takes you back to LoginScreen.
A quick word of caution
If you don't handle `popBackStack()` or navigation options (like `popUpTo`) after moving from the Login screen to the Home screen, pressing Back on the Home screen can send the user right back to the Login screen (even though they're already logged in) — you'll want to fix this UX gap in a production app.