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 three 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

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 the Safe Args Gradle plugin which provides type safety when navigating and passing data between destinations.
  • 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.7.7"

  // Java language implementation
  implementation "androidx.navigation:navigation-fragment:$nav_version"
  implementation "androidx.navigation:navigation-ui:$nav_version"

  // Kotlin
  implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
  implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

  // Feature module Support
  implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

  // Testing Navigation
  androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

  // Jetpack Compose Integration
  implementation "androidx.navigation:navigation-compose:$nav_version"
}

Kotlin

dependencies {
  val nav_version = "2.7.7"

  // Java language implementation
  implementation("androidx.navigation:navigation-fragment:$nav_version")
  implementation("androidx.navigation:navigation-ui:$nav_version")

  // Kotlin
  implementation("androidx.navigation:navigation-fragment-ktx:$nav_version")
  implementation("androidx.navigation:navigation-ui-ktx:$nav_version")

  // Feature module Support
  implementation("androidx.navigation:navigation-dynamic-features-fragment:$nav_version")

  // Testing Navigation
  androidTestImplementation("androidx.navigation:navigation-testing:$nav_version")

  // Jetpack Compose Integration
  implementation("androidx.navigation:navigation-compose:$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