Navigation

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.

NavGraph

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.

NavController

Destination

A node in the navigation graph. When the user navigates to this node, the host displays its content.

NavDestination

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

dependencies {
  def nav_version = "2.8.4"

  // 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"

}

Kotlin

dependencies {
  val nav_version = "2.8.4"

  // 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")

}

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:

Codelabs

Videos

Samples