NavController

public class NavController

Known direct subclasses
NavHostController

Subclass of NavController that offers additional APIs for use by a NavHost to connect the NavController to external dependencies.

Known indirect subclasses
TestNavHostController

Subclass of NavHostController that offers additional APIs for testing Navigation.


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 types

NavController.Companion
NavController.OnDestinationChangedListener

OnDestinationChangedListener receives a callback when the currentDestination or its arguments change.

Public fields

@Nullable NavBackStackEntry

The topmost NavBackStackEntry.

final @NonNull Flow<@NonNull NavBackStackEntry>

A Flow that will emit the currently active NavBackStackEntry whenever it changes.

@Nullable NavDestination

The current destination.

@NonNull NavGraph

The topmost navigation graph associated with this NavController.

@NonNull NavInflater

The inflater for this controller.

@NonNull NavigatorProvider

The NavController's NavigatorProvider.

@Nullable NavBackStackEntry

The previous visible NavBackStackEntry.

final @NonNull StateFlow<@NonNull List<@NonNull NavBackStackEntry>>

A StateFlow that will emit the currently visible NavBackStackEntries whenever they change.

Public constructors

Public methods

void

Adds an OnDestinationChangedListener to this controller to receive a callback whenever the currentDestination or its arguments change.

final @MainThread boolean

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

final @MainThread 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.

@NonNull NavDeepLinkBuilder

Create a deep link to a destination within this NavController.

@NonNull NavBackStackEntry
getBackStackEntry(@IdRes int destinationId)

Gets the topmost NavBackStackEntry for a destination id.

final @NonNull NavBackStackEntry

Gets the topmost NavBackStackEntry for a route.

@NonNull ViewModelStoreOwner
getViewModelStoreOwner(@IdRes int navGraphId)

Gets the ViewModelStoreOwner for a NavGraph.

@MainThread boolean

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

@MainThread void
navigate(@IdRes int resId)

Navigate to a destination from the current navigation graph.

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

Navigate to a destination from the current navigation graph.

@MainThread void