Lecture 7
Lecture 7
Compose
Author:Semen Naduiev
What is Compose Navigation?
Compose Navigation is a Jetpack library for handling navigation in Jetpack Compose apps.
NavController: Responsible for navigating between destinations—that is, the screens in your
app.
NavHost: Composable acting as a container for displaying the current destination of the
NavGraph.
Define routes for destinations in your app
One of the fundamental concepts of navigation in a Compose app is the route. A route is a string
that corresponds to a destination. This idea is similar to the concept of a URL. Just as a different
URL maps to a different page on a website, a route is a string that maps to a destination and
serves as its unique identifier. A destination is typically a single Composable or group of
Composables corresponding to what the user sees. The Cupcake app needs destinations for the
start order screen, the flavor screen, the pickup date screen, and the order summary screen.
There are a finite number of screens in an app, so there are also a finite number of routes. You
can define an app's routes using an enum or sealed(I recommend) class.
Understanding the Navigation Backstack
Why is it important?
navController.navigate("home") {
popUpTo(0)
}
Preventing Duplicate Screens in Backstack
navController.navigate("profile") {
launchSingleTop = true
}
Passing Arguments Without Adding a New
Screen to the Stack
Be carefull
🔹 Jetpack Compose provides BackHandler to handle the system back press event.
🔹 Useful for confirming exit, preventing unintended navigation, or handling nested screens.
Basic Usage of BackHandler
Conditional Back Handling
BackHandler in Nested Navigation