NavController

open class NavController
kotlin.Any
   ↳ androidx.navigation.NavController

NavController manages app navigation within a NavHost.

Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly.

Navigation flows and destinations are determined by the navigation graph owned by the controller. These graphs are typically inflated from an Android resource, but, like views, they can also be constructed or combined programmatically or for the case of dynamic navigation structure. (For example, if the navigation structure of the application is determined by live data obtained' from a remote server.)

Summary

Nested classes
abstract

OnNavigatorNavigatedListener receives a callback when the associated controller navigates to a new destination.

Constants
static String

The Intent that triggered a deep link to the current destination.

Public constructors
<init>(context: Context)

Constructs a new controller for a given Context.

Public methods
open Boolean

Checks the given Intent for a Navigation deep link and navigates to the deep link if present.

open NavGraph!

Gets the topmost navigation graph associated with this NavController.

open Unit

Adds an OnNavigatedListener to this controller to receive events when the controller navigates to a new destination.

open NavInflater

Returns the inflater for this controller.

open NavDeepLinkBuilder

Create a deep link to a destination within this NavController.

open Boolean

Attempts to navigate up in the navigation hierarchy.

Unit
navigate(resId: Int)

Navigate to a destination from the current navigation graph.

Unit
navigate(resId: Int, args: Bundle?)

Navigate to a destination from the current navigation graph.

open Unit
navigate(resId: Int, args: Bundle?, navOptions: NavOptions?)

Navigate to a destination from the current navigation graph.

open Unit
navigate(directions: NavDirections)

Navigate via the given NavDirections

open Unit
navigate(directions: NavDirections, navOptions: NavOptions?)

Navigate via the given NavDirections

open Unit
restoreState(navState: Bundle?)

Restores all navigation controller state from a bundle.

open Bundle?

Saves all navigation controller state to a Bundle.

open Unit

Removes an OnNavigatedListener from this controller.

open Boolean

Attempts to pop the controller's back stack.

open Boolean
popBackStack(destinationId: Int, inclusive: Boolean)

Attempts to pop the controller's back stack back to a specific destination.

open NavDestination!

Gets the current destination.

open Unit

Sets the navigation graph as specified in the application manifest.

open Unit
setGraph(graphResId: Int)

Sets the navigation graph to the specified resource.

open Unit

Sets the navigation graph to the specified graph.

open NavigatorProvider

Retrieve the NavController's NavigatorProvider.

Extension functions
From androidx.navigation
NavGraph
NavController.createGraph(id: Int = 0, startDestination: Int, block: NavGraphBuilder.() -> Unit)

Construct a new NavGraph

Constants

KEY_DEEP_LINK_INTENT

static val KEY_DEEP_LINK_INTENT: String

The Intent that triggered a deep link to the current destination.

Value: "android-support-nav:controller:deepLinkIntent"

Public constructors

<init>

NavController(context: Context)

Constructs a new controller for a given Context. Controllers should not be used outside of their context and retain a hard reference to the context supplied. If you need a global controller, pass Context#getApplicationContext().

Apps should generally not construct controllers, instead obtain a relevant controller directly from a navigation host via NavHost#getNavController() or by using one of the utility methods on the Navigation class.

Note that controllers that are not constructed with an Activity context (or a wrapped activity context) will only be able to navigate to new tasks or new document tasks when navigating to new activities.

Parameters
context Context: context for this controller

Public methods

onHandleDeepLink

open fun onHandleDeepLink(intent: Intent?): Boolean

Checks the given Intent for a Navigation deep link and navigates to the deep link if present. This is called automatically for you the first time you set the graph if you've passed in an Activity as the context when constructing this NavController, but should be manually called if your Activity receives new Intents in Activity#onNewIntent(Intent).

The types of Intents that are supported include:

The navigation graph should be set before calling this method.

Parameters
intent Intent?: The Intent that may contain a valid deep link
Return
Boolean: True if the navigation controller found a valid deep link and navigated to it.

getGraph

open fun getGraph(): NavGraph!

Gets the topmost navigation graph associated with this NavController.

addOnNavigatedListener

open fun addOnNavigatedListener(listener: NavController.OnNavigatedListener): Unit

Adds an OnNavigatedListener to this controller to receive events when the controller navigates to a new destination.

The current destination, if any, will be immediately sent to your listener.

Parameters
listener NavController.OnNavigatedListener: the listener to receive events

getNavInflater

open fun getNavInflater(): NavInflater

Returns the inflater for this controller.

Return
NavInflater: inflater for loading navigation resources

createDeepLink

open fun createDeepLink(): NavDeepLinkBuilder

Create a deep link to a destination within this NavController.

Return
NavDeepLinkBuilder: a NavDeepLinkBuilder suitable for constructing a deep link

navigateUp

open fun navigateUp(): Boolean

Attempts to navigate up in the navigation hierarchy. Suitable for when the user presses the "Up" button marked with a left (or start)-facing arrow in the upper left (or starting) corner of the app UI.

The intended behavior of Up differs from Back when the user did not reach the current destination from the application's own task. e.g. if the user is viewing a document or link in the current app in an activity hosted on another app's task where the user clicked the link. In this case the current activity (determined by the context used to create this NavController) will be finished and the user will be taken to an appropriate destination in this app on its own task.

Return
Boolean: true if navigation was successful, false otherwise

navigate

fun navigate(resId: Int): Unit

Navigate to a destination from the current navigation graph. This supports both navigating via an action and directly navigating to a destination.

Parameters
resId Int: an action id or a destination id to navigate to

navigate

fun navigate(resId: Int, args: Bundle?): Unit

Navigate to a destination from the current navigation graph. This supports both navigating via an action and directly navigating to a destination.

Parameters
resId Int: an action id or a destination id to navigate to
args Int: arguments to pass to the destination

navigate

open fun navigate(resId: Int, args: Bundle?, navOptions: NavOptions?): Unit

Navigate to a destination from the current navigation graph. This supports both navigating via an action and directly navigating to a destination.

Parameters
resId Int: an action id or a destination id to navigate to
args Int: arguments to pass to the destination
navOptions Int: special options for this navigation operation

navigate

open fun navigate(directions: NavDirections): Unit

Navigate via the given NavDirections

Parameters
directions NavDirections: directions that describe this navigation operation

navigate

open fun navigate(directions: NavDirections, navOptions: NavOptions?): Unit

Navigate via the given NavDirections

Parameters
directions NavDirections: directions that describe this navigation operation

restoreState

open fun restoreState(navState: Bundle?): Unit

Restores all navigation controller state from a bundle.

State may be saved to a bundle by calling #saveState(). Restoring controller state is the responsibility of a NavHost.

Parameters
navState Bundle?: state bundle to restore

saveState

open fun saveState(): Bundle?

Saves all navigation controller state to a Bundle.

State may be restored from a bundle returned from this method by calling #restoreState(Bundle). Saving controller state is the responsibility of a NavHost.

Return
Bundle?: saved state for this controller

removeOnNavigatedListener

open fun removeOnNavigatedListener(listener: NavController.OnNavigatedListener): Unit

Removes an OnNavigatedListener from this controller. It will no longer receive navigation events.

Parameters
listener NavController.OnNavigatedListener: the listener to remove

popBackStack

open fun popBackStack(): Boolean

Attempts to pop the controller's back stack. Analogous to when the user presses the system Back button when the associated navigation host has focus.

Return
Boolean: true if the stack was popped, false otherwise

popBackStack

open fun popBackStack(destinationId: Int, inclusive: Boolean): Boolean

Attempts to pop the controller's back stack back to a specific destination.

Parameters
destinationId Int: The topmost destination to retain
inclusive Int: Whether the given destination should also be popped.
Return
Boolean: true if the stack was popped at least once, false otherwise

getCurrentDestination

open fun getCurrentDestination(): NavDestination!

Gets the current destination.

setMetadataGraph

open fun setMetadataGraph(): Unit

Sets the navigation graph as specified in the application manifest.

Applications may declare a graph resource in their manifest instead of declaring or passing this data to each host or controller:

  

The inflated graph can be retrieved via #getGraph().

setGraph

open fun setGraph(graphResId: Int): Unit

Sets the navigation graph to the specified resource. Any current navigation graph data will be replaced.

The inflated graph can be retrieved via #getGraph().

Parameters
graphResId Int: resource id of the navigation graph to inflate

setGraph

open fun setGraph(graph: NavGraph): Unit

Sets the navigation graph to the specified graph. Any current navigation graph data will be replaced.

The graph can be retrieved later via #getGraph().

Parameters
graph NavGraph: graph to set

getNavigatorProvider

open fun getNavigatorProvider(): NavigatorProvider

Retrieve the NavController's NavigatorProvider. All Navigators used to construct the navigation graph for this nav controller should be added to this navigator provider before the graph is constructed.

Generally, the Navigators are set for you by the NavHost hosting this NavController and you do not need to manually interact with the navigator provider.

Return
NavigatorProvider: The NavigatorProvider used by this NavController.