Navigation Compose
Android Development Fundamentals with Compose
Course 4 ยท Chapter 7 ยท Moving Between Screens
A real app has several screens. In a Compose app you usually keep one Activity and swap which composable fills it. The Navigation Compose library does the swapping: a NavController remembers where the user has been,
and a NavHost says which composable belongs to which route. This chapter builds a two-screen app with an argument, then covers the back stack, bottom navigation and deep links.
rememberNavController(), the NavHost and composable destinations, string routes with navArgument, screens taking () -> Unit callbacks that the NavHost wires to navigate(),
popBackStack() (including its Boolean result), popUpTo with inclusive, saveState and restoreState, typed routes (@Serializable, toRoute(), version 2.8 and later), and Navigation 3.
Not found in the docs searches: the exact dependency version, NavigationBar and currentBackStackEntryAsState, launchSingleTop, and deep-link setup (general knowledge). All code is unrun (no Kotlin compiler on the machine used to write this). Nothing was seen on your install.
NavDisplay draws the matching screens. This chapter teaches Navigation Compose (NavHost),
because it is what most existing Compose apps use and what the course outline set. For a brand-new app, check the current docs and whether Google recommends Navigation 3; I have not compared them in depth. If you want this chapter, or a later one, rewritten for Navigation 3, say so.
Add the Dependency
The Empty Activity template does not include Navigation Compose. Add it through the version catalog (Android Studio 2: The New UI 4 explains the catalog), then sync. Use the version Android Studio or the release notes suggest; I have not pinned one here.
Two Screens and an Argument
(docs) The recommended shape is that each screen is a composable taking plain callbacks, and the NavHost supplies lambdas that call navigate(). A screen therefore never touches the controller, which keeps it easy to preview and test (Chapter 4's stateless idea again).
| Part | What it does |
|---|---|
rememberNavController() | (docs) Gives you the NavHostController, which moves between screens. |
NavHost(startDestination = ...) | Holds the navigation graph and shows the current destination. |
composable("home") { } | Registers a route and the composable to show for it. |
"detail/{name}" | (docs) A route with an argument placeholder, declared with navArgument. |
navController.navigate("detail/$name") | Goes to that route, pushing it on the back stack. |
navController.popBackStack() | (docs) Goes back; returns a Boolean, false when there was nothing to pop (for example the start destination was already removed). |
The Back Stack
navigate pushes a screen; the system Back button, or popBackStack(), removes the top one. (docs) To clear part of the stack while navigating, use popUpTo; its inclusive flag decides whether the named destination is removed too.
Bottom Navigation
A bar of tabs along the bottom is a NavigationBar in a Scaffold. (docs) The documented pattern for keeping each tab's place uses popUpTo with saveState, and restoreState on the way back; the rest is general knowledge.
Deep Links
(general knowledge; not found by my search) A deep link lets a web address or notification open one specific screen. You add a navDeepLink to the destination and a matching intent filter to the Activity in the manifest. This chapter does not build one; treat the snippet as a pointer, and read the official deep-link guide before using it.
Newer Ways to Write Routes
- Typed routes (docs): instead of strings, routes are
@Serializableclasses or objects, you callnavigatewith an instance, and a destination reads its arguments withtoRoute(). The docs say the typed overloads arrived in versions 2.8 and 2.9, so check your library version. They need the Kotlin serialization plugin, so they are not used in this chapter. - Navigation 3 (docs): see the box above.
Compared with the XML Way
| Navigation Component with Views (Android Development Fundamentals) | Navigation Compose | |
|---|---|---|
| The graph | A navigation XML file edited visually | Code inside NavHost { } |
| Destinations | Fragments | Composables |
| Passing data | SafeArgs generated classes | Route arguments (or typed routes) |
| Going back | popBackStack() | popBackStack() (the same name) |
| Deep links | In the graph XML | navDeepLink in code |
Try It Yourself
Add the dependency, then build the two-screen app: Home with two buttons, and a Detail screen that shows the name passed in the route. Check that the Back button returns to Home.
๐ View solutionAdd a third screen, "Done", reached from Detail. From Done, add a button that returns to Home and clears the stack so that pressing Back leaves the app instead of returning to Detail.
๐ View solutionBuild a bottom navigation bar with three tabs. Open a tab, scroll or change something on it, switch tabs and come back. Say whether the tab remembers its state, and which flags you used.
๐ View solutionrememberNavController; NavHost and composable destinations; string routes with navArgument; the callback pattern; popBackStack and its Boolean; popUpTo with inclusive; saveState and restoreState; typed routes from 2.8; Navigation 3's NavDisplay and key list.Not found in the docs searches: the dependency's current version and artifact details beyond its name;
NavigationBar, NavigationBarItem, currentBackStackEntryAsState, launchSingleTop; deep-link setup; the XML comparison column (general knowledge).Not verified: whether Navigation 3 is now the recommended choice for new apps (not compared); the imports; whether the bottom-bar code and the pop-up flags behave as described; every listing and solution (unrun); the exercises (not run by me). Nothing was checked on your install.
Sources: Navigation and the back stack (developer.android.com), Navigate between screens with Compose (developer.android.com codelab), androidx.navigation.compose (developer.android.com), Navigation 3 release notes (developer.android.com), Navigation release notes (developer.android.com).