NavController  |  API reference  |  Android Developers

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 navInflater 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.)

Constants

Public constructors

Public methods

clearBackStack

@MainThread
public final boolean <T extends Object> clearBackStack()

Clears any saved state associated with KClass T that was previously saved via popBackStack when using a saveState value of true.

Parameters
<T extends Object>

The route from the KClass of the destination previously used with popBackStack with a saveStatevalue of true. The target NavDestination must have been created with route from KClass.

Returns
boolean

true if the saved state of the stack associated with T was cleared.

clearBackStack

@MainThread
public final boolean clearBackStack(@IdRes int destinationId)

Clears any saved state associated with destinationId that was previously saved via popBackStack when using a saveState value of true.

Parameters
@IdRes int destinationId

The ID of the destination previously used with popBackStack with a saveStatevalue of true

Returns
boolean

true if the saved state of the stack associated with destinationId was cleared.

clearBackStack

@MainThread
public final boolean <T extends Object> clearBackStack(@NonNull T route)

Clears any saved state associated with KClass T that was previously saved via popBackStack when using a saveState value of true.

Parameters
@NonNull T route

The route from an Object of the destination previously used with popBackStack with a saveStatevalue of true. The target NavDestination must have been created with route from KClass.

Returns
boolean

true if the saved state of the stack associated with T was cleared.

clearBackStack

@MainThread
public final boolean clearBackStack(@NonNull String route)

Clears any saved state associated with route that was previously saved via popBackStack when using a saveState value of true.

Parameters
@NonNull String route

The route of the destination previously used with popBackStack with a saveState value of true. May contain filled in arguments as long as it is exact match with route used with popBackStack.

Returns
boolean

true if the saved state of the stack associated with route was cleared.

getGraph

@MainThread
public @NonNull NavGraph getGraph()

The topmost navigation graph associated with this NavController.

When this is set any current navigation graph data (including back stack) will be replaced.

getNavigatorProvider

public @NonNull NavigatorProvider getNavigatorProvider()

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.

This can only be set before the graph is set via setGraph().

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.

getVisibleEntries

public final @NonNull StateFlow<@NonNull List<@NonNull NavBackStackEntry>> getVisibleEntries()

A StateFlow that will emit the currently visible NavBackStackEntries whenever they change. If there is no visible NavBackStackEntry, this will be set to an empty list.

  • CREATED entries are listed first and include all entries that are in the process of completing their exit transition. Note that this can include entries that have been popped off the Navigation back stack.

  • STARTED entries on the back stack are next and include all entries that are running their enter transition and entries whose destination is partially covered by a FloatingWindow destination

  • The last entry in the list is the topmost entry in the back stack and is in the RESUMED state only if its enter transition has completed. Otherwise it too will be STARTED.

Note that the Lifecycle of any entry cannot be higher than the containing Activity/Fragment - if the Activity is not RESUMED, no entry will be RESUMED, no matter what the transition state is.

handleDeepLink

@MainThread
public boolean handleDeepLink(Intent intent)

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.

The types of Intents that are supported include:

Intents created by NavDeepLinkBuilder or createDeepLink. This assumes that the current graph shares the same hierarchy to get to the deep linked destination as when the deep link was constructed. Intents that include a data Uri. This Uri will be checked against the Uri patterns in the NavDeepLinks added via NavDestination.addDeepLink.

The navigation graph should be set before calling this method.

Parameters
Intent intent

The Intent that may contain a valid deep link

Returns
boolean

True if the navigation controller found a valid deep link and navigated to it.

handleDeepLink

@MainThread
public final boolean handleDeepLink(@NonNull NavDeepLinkRequest request)

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

The navigation graph should be set before calling this method.

Returns
boolean

True if the navigation controller found a valid deep link and navigated to it.

@MainThread
public void navigate(@IdRes int resId)

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

Parameters
@IdRes int resId

an action id or a destination id to navigate to

@MainThread
public void navigate(@IdRes int resId, Bundle args)

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

Parameters
@IdRes int resId

an action id or a destination id to navigate to

Bundle args

arguments to pass to the destination

@MainThread
public void navigate(@IdRes int resId, Bundle args, NavOptions navOptions)

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

If given NavOptions pass in NavOptions.restoreState true, any args passed here will be overridden by the restored args.

Parameters
@IdRes int resId

an action id or a destination id to navigate to

Bundle args

arguments to pass to the destination

NavOptions navOptions

special options for this navigation operation

@MainThread
public final void <T extends Object> navigate(
    @NonNull T route,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)

Navigate to a route from an Object in the current NavGraph. If an invalid route is given, an IllegalArgumentException will be thrown.

The target NavDestination must have been created with route from a KClass

If given NavOptions pass in NavOptions.restoreState true, any args passed here as part of the route will be overridden by the restored args.

@MainThread
public boolean navigateUp()

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 Activity.finish and the user will be taken to an appropriate destination in this app on its own task.

Returns
boolean

true if navigation was successful, false otherwise

popBackStack

@MainThread
public boolean popBackStack()

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

Returns
boolean

true if the stack was popped at least once and the user has been navigated to another destination, false otherwise

popBackStack

@MainThread
public boolean popBackStack(@IdRes int destinationId, boolean inclusive)

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

Parameters
@IdRes int destinationId

The topmost destination to retain

boolean inclusive

Whether the given destination should also be popped.

Returns
boolean

true if the stack was popped at least once and the user has been navigated to another destination, false otherwise

popBackStack

@MainThread
public final boolean <T extends Object> popBackStack(boolean inclusive, boolean saveState)

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

Parameters
<T extends Object>

The topmost destination to retain with route from a KClass. The target NavDestination must have been created with route from KClass.

boolean inclusive

Whether the given destination should also be popped.

boolean saveState

Whether the back stack and the state of all destinations between the current destination and T should be saved for later restoration via NavOptions.Builder.setRestoreState or the restoreState attribute using the same T (note: this matching ID is true whether inclusive is true or false).

Returns
boolean

true if the stack was popped at least once and the user has been navigated to another destination, false otherwise

popBackStack

@MainThread
public boolean popBackStack(
    @IdRes int destinationId,
    boolean inclusive,
    boolean saveState
)

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

Parameters
@IdRes int destinationId

The topmost destination to retain

boolean inclusive

Whether the given destination should also be popped.

boolean saveState

Whether the back stack and the state of all destinations between the current destination and the destinationId should be saved for later restoration via NavOptions.Builder.setRestoreState or the restoreState attribute using the same destinationId (note: this matching ID is true whether inclusive is true or false).

Returns
boolean

true if the stack was popped at least once and the user has been navigated to another destination, false otherwise

popBackStack

@MainThread
public final boolean <T extends Object> popBackStack(
    @NonNull KClass<@NonNull T> route,
    boolean inclusive,
    boolean saveState
)

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

Parameters
@NonNull KClass<@NonNull T> route

The topmost destination to retain with route from a KClass. The target NavDestination must have been created with route from KClass.

boolean inclusive

Whether the given destination should also be popped.

boolean saveState

Whether the back stack and the state of all destinations between the current destination and route should be saved for later restoration via NavOptions.Builder.setRestoreState or the restoreState attribute using the same T (note: this matching ID is true whether inclusive is true or false).

Returns
boolean

true if the stack was popped at least once and the user has been navigated to another destination, false otherwise

popBackStack

@MainThread
public final boolean <T extends Object> popBackStack(
    @NonNull T route,
    boolean inclusive,
    boolean saveState
)

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

Parameters
@NonNull T route

The topmost destination to retain with route from an Object. The target NavDestination must have been created with route from KClass.

boolean inclusive

Whether the given destination should also be popped.

boolean saveState

Whether the back stack and the state of all destinations between the current destination and the route should be saved for later restoration via NavOptions.Builder.setRestoreState or the restoreState attribute using the same route (note: this matching ID is true whether inclusive is true or false).

Returns
boolean

true if the stack was popped at least once and the user has been navigated to another destination, false otherwise

popBackStack

@MainThread
public final boolean popBackStack(@NonNull String route, boolean inclusive, boolean saveState)

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

Parameters
@NonNull String route

The topmost destination to retain. May contain filled in arguments as long as it is exact match with route used to navigate.

boolean inclusive

Whether the given destination should also be popped.

boolean saveState

Whether the back stack and the state of all destinations between the current destination and the route should be saved for later restoration via NavOptions.Builder.setRestoreState or the restoreState attribute using the same route (note: this matching ID is true whether inclusive is true or false).

Returns
boolean

true if the stack was popped at least once and the user has been navigated to another destination, false otherwise

restoreState

@CallSuper
public void restoreState(Bundle navState)

Restores all navigation controller state from a SavedState. This should be called before any call to setGraph.

State may be saved to a SavedState by calling saveState. Restoring controller state is the responsibility of a NavHost.

Parameters
Bundle navState

SavedState to restore

restoreState

@CallSuper
public void restoreState(SavedState navState)

Restores all navigation controller state from a SavedState. This should be called before any call to setGraph.

State may be saved to a SavedState by calling saveState. Restoring controller state is the responsibility of a NavHost.

Parameters
SavedState navState

SavedState to restore

saveState

@CallSuper
public SavedState saveState()

Saves all navigation controller state to a SavedState.

State may be restored from a SavedState returned from this method by calling restoreState. Saving controller state is the responsibility of a NavHost.

Returns
SavedState

saved state for this controller

setGraph

@MainThread
@CallSuper
public void setGraph(@NonNull NavGraph value)

The topmost navigation graph associated with this NavController.

When this is set any current navigation graph data (including back stack) will be replaced.

setGraph

@MainThread
@CallSuper
public void setGraph(@NavigationRes int graphResId, Bundle startDestinationArgs)

Sets the navigation graph to the specified resource. Any current navigation graph data (including back stack) will be replaced.

The inflated graph can be retrieved via graph.

Parameters
@NavigationRes int graphResId

resource id of the navigation graph to inflate

Bundle startDestinationArgs

arguments to send to the start destination of the graph

Extension functions