Navigation refers to the interactions that let users navigate across, into, and back out from the different pieces of content within your app.
Android Jetpack's Navigation component includes the Navigation library, Safe Args Gradle plug-in, and tooling to help you implement app navigation. The Navigation component handles diverse navigation use cases, from straightforward button clicks to more complex patterns, such as app bars and the navigation drawer.
Key concepts
The following table provides an overview of the key concepts in navigation and the main types that you use to implement them.
Concept |
Purpose |
Type |
---|---|---|
Host |
A UI element that contains the current navigation destination. That is, when a user navigates through an app, the app essentially swaps destinations in and out of the navigation host. |
|
Graph |
A data structure that defines all the navigation destinations within the app and how they connect together. |
|
Controller |
The central coordinator for managing navigation between destinations. The controller offers methods for navigating between destinations, handling deep links, managing the back stack, and more. |
|
Destination |
A node in the navigation graph. When the user navigates to this node, the host displays its content. |
Typically created when constructing the navigation graph. |
Route |
Uniquely identifies a destination and any data required by it. You can navigate using routes. Routes take you to destinations. |
Any serializable data type. |
Benefits and features
The Navigation component provides a number of other benefits and features, including the following:
- Animations and transitions: Provides standardized resources for animations and transitions.
- Deep linking: Implements and handles deep links that take the user directly to a destination.
- UI patterns: Supports patterns such as navigation drawers and bottom navigation with minimal additional work.
- Type safety: Includes support for passing data between destinations with type safety.
- ViewModel support: Enables scoping a
ViewModel
to a navigation graph to share UI-related data between the graph's destinations. - Fragment transactions: Fully supports and handles fragment transactions.
- Back and up: Handles back and up actions correctly by default.
Set up your environment
To include navigation support in your project, add the following dependencies to
your app's build.gradle
file:
Groovy
plugins { // Kotlin serialization plugin for type safe routes and navigation arguments id 'org.jetbrains.kotlin.plugin.serialization' version '2.0.21' } dependencies { def nav_version = "2.8.5" // Jetpack Compose Integration implementation "androidx.navigation:navigation-compose:$nav_version" // Views/Fragments Integration implementation "androidx.navigation:navigation-fragment:$nav_version" implementation "androidx.navigation:navigation-ui:$nav_version" // Feature module support for Fragments implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version" // Testing Navigation androidTestImplementation "androidx.navigation:navigation-testing:$nav_version" // JSON serialization library, works with the Kotlin serialization plugin. implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3" }
Kotlin
plugins { // Kotlin serialization plugin for type safe routes and navigation arguments kotlin("plugin.serialization") version "2.0.21" } dependencies { val nav_version = "2.8.5" // Jetpack Compose integration implementation("androidx.navigation:navigation-compose:$nav_version") // Views/Fragments integration implementation("androidx.navigation:navigation-fragment:$nav_version") implementation("androidx.navigation:navigation-ui:$nav_version") // Feature module support for Fragments implementation("androidx.navigation:navigation-dynamic-features-fragment:$nav_version") // Testing Navigation androidTestImplementation("androidx.navigation:navigation-testing:$nav_version") // JSON serialization library, works with the Kotlin serialization plugin implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3") }
For information on adding other architecture components to your project, see Add components to your project.
Next steps
For more documentation and resources related to the Navigation component, see the following resources.
Detailed guides
For more information on how to implement a navigation host and NavController
,
as well as detail on how they interact with Compose and other UI frameworks, see
the following guides:
- Create a navigation controller: Outlines how to create a
NavController
. - Create your navigation graph: Details how to create a navigation host and a navigation graph.
- Navigate to a destination: Demonstrates how to use a
NavController
to move between the destinations in your graph.
Codelabs
- Learn Jetpack Navigation
- Fragments and the Navigation Component
- Build an adaptive app with dynamic navigation
Videos
- Navigating navigation
- 10 best practices for moving to a single activity
- Single activity: Why, when, and how (Android Dev Summit '18)
- Android Jetpack: Manage UI navigation with navigation controller (Google I/O '18)