lightbulb_outline Help shape the future of the Google Play Console, Android Studio, and Firebase. Start survey

androidx.navigation

Navigation is a framework for navigating between 'destinations' within an Android application that provides a consistent API whether destinations are implemented as Fragments, Activities, or other components.

There are 3 major components in Navigation.

Below is an example of working with a NavController.
 // File: HomeFragment.java public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { // For example purposes, assume our layout created in onCreateView has a Button // that should navigate the user to a destination Button b = view.findViewById(R.id.view_details); // Retrieve the NavController from any View within a NavHost final NavController navController = Navigation.findNavController(this); // And set the listener b.setOnClickListener(() -%gt; navController.navigate(R.id.details)); // Or use the convenience method in Navigation to combine all of the previous steps b.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.details)); } 

Please see the documentation of individual classes for details.

Classes

ActivityNavigator

ActivityNavigator implements cross-activity navigation.

ActivityNavigatorDestinationBuilder

DSL for constructing a new ActivityNavigator.Destination

AnimBuilder

DSL for setting custom Animation or Animator resources on a NavOptionsBuilder

NavAction

Navigation actions provide a level of indirection between your navigation code and the underlying destinations.

NavActionBuilder

DSL for building a NavAction.

NavController

NavController manages app navigation within a NavHost.

NavDeepLinkBuilder

Class used to construct deep links to a particular destination in a NavGraph.

NavDestination

NavDestination represents one node within an overall navigation graph.

NavDestinationBuilder

DSL for constructing a new NavDestination

NavGraph

NavGraph is a collection of NavDestination nodes fetchable by ID.

NavGraphBuilder

DSL for constructing a new NavGraph

NavGraphNavigator

A Navigator built specifically for NavGraph elements.

Navigation

Entry point for navigation operations.

Navigator

Navigator defines a mechanism for navigating within an app.

NavInflater

Class which translates a navigation XML file into a NavGraph

NavOptions

NavOptions stores special options for navigate actions

NavOptionsBuilder

DSL for constructing a new NavOptions

PopUpToBuilder

DSL for customizing NavOptionsBuilder.popUpTo operations.

Annotations

NavDestinationDsl

NavOptionsDsl

Top-level functions summary

NavOptions

Construct a new NavOptions

Extension functions summary

For android.app.Activity
NavController

Find a NavController given the id of a View and its containing Activity.

For android.view.View
NavController

Find a NavController associated with a View.

For NavGraphBuilder
Unit

Construct a new ActivityNavigator.Destination

Unit
NavGraphBuilder.navigation(id: Int, startDestination: Int, block: NavGraphBuilder.() -> Unit)

Construct a nested NavGraph

For NavController
NavGraph
NavController.createGraph(id: Int = 0, startDestination: Int, block: NavGraphBuilder.() -> Unit)

Construct a new NavGraph

For NavHost
NavGraph
NavHost.createGraph(id: Int = 0, startDestination: Int, block: NavGraphBuilder.() -> Unit)

Construct a new NavGraph

For NavGraph
operator NavDestination

Returns the destination with id.

operator Boolean

Returns true if a destination with id is found in this navigation graph.

operator Unit

Adds a destination to this NavGraph.

operator Unit

Add all destinations from another collection to this one.

operator Unit

Removes node from this navigation graph.

For NavigatorProvider
operator T

Retrieves a registered Navigator by name.

operator T

Retrieves a registered Navigator using the name provided by the Navigator.Name annotation.

operator Unit

Register a navigator using the name provided by the Navigator.Name annotation.

NavGraph
NavigatorProvider.navigation(id: Int = 0, startDestination: Int, block: NavGraphBuilder.() -> Unit)

Construct a new NavGraph

operator Navigator<out NavDestination!>?
NavigatorProvider.set(name: String, navigator: Navigator<D>)

Register a Navigator by name.

Top-level functions

navOptions

fun navOptions(block: NavOptionsBuilder.() -> Unit): NavOptions

Construct a new NavOptions

Extension functions

activity

inline fun NavGraphBuilder.activity(id: Int, block: ActivityNavigatorDestinationBuilder.() -> Unit): Unit

Construct a new ActivityNavigator.Destination

contains

operator fun NavGraph.contains(id: Int): Boolean

Returns true if a destination with id is found in this navigation graph.

createGraph

inline fun NavController.createGraph(id: Int = 0, startDestination: Int, block: NavGraphBuilder.() -> Unit): NavGraph

Construct a new NavGraph

createGraph

inline fun NavHost.createGraph(id: Int = 0, startDestination: Int, block: NavGraphBuilder.() -> Unit): NavGraph

Construct a new NavGraph

findNavController

fun Activity.findNavController(viewId: Int): NavController

Find a NavController given the id of a View and its containing Activity.

Calling this on a View that is not a NavHost or within a NavHost will result in an IllegalStateException

findNavController

fun View.findNavController(): NavController

Find a NavController associated with a View.

Calling this on a View not within a NavHost will result in an IllegalStateException

get

inline operator fun NavGraph.get(id: Int): NavDestination

Returns the destination with id.

Exceptions
IllegalArgumentException if no destination is found with that id.

get

inline operator fun <D : NavDestination, T : Navigator<D>> NavigatorProvider.get(name: String): T

Retrieves a registered Navigator by name.

Exceptions
IllegalStateException if the Navigator has not been added

get

inline operator fun <D : NavDestination, T : Navigator<D>> NavigatorProvider.get(clazz: KClass<T>): T

Retrieves a registered Navigator using the name provided by the Navigator.Name annotation.

Exceptions
IllegalStateException if the Navigator has not been added

minusAssign

inline operator fun NavGraph.minusAssign(node: NavDestination): Unit

Removes node from this navigation graph.

navigation

inline fun NavGraphBuilder.navigation(id: Int, startDestination: Int, block: NavGraphBuilder.() -> Unit): Unit

Construct a nested NavGraph

navigation

inline fun NavigatorProvider.navigation(id: Int = 0, startDestination: Int, block: NavGraphBuilder.() -> Unit): NavGraph

Construct a new NavGraph

plusAssign

inline operator fun NavGraph.plusAssign(node: NavDestination): Unit

Adds a destination to this NavGraph. The destination must have an id set.

The destination must not have a parent set. If the destination is already part of a NavGraph, call NavGraph.remove before calling this method.

Parameters
node destination to add

plusAssign

inline operator fun NavGraph.plusAssign(other: NavGraph): Unit

Add all destinations from another collection to this one. As each destination has at most one parent, the destinations will be removed from the given NavGraph.

Parameters
other collection of destinations to add. All destinations will be removed from the parameter graph after being added to this graph.

plusAssign

inline operator fun <D : NavDestination> NavigatorProvider.plusAssign(navigator: Navigator<D>): Unit

Register a navigator using the name provided by the Navigator.Name annotation.

set

inline operator fun <D : NavDestination> NavigatorProvider.set(name: String, navigator: Navigator<D>): Navigator<out NavDestination!>?

Register a Navigator by name. If a navigator by this name is already registered, this new navigator will replace it.

Return
the previously added Navigator for the given name, if any