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.

Public constructors

Public methods

void

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

@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
navigate(
    @IdRes int resId,
    @Nullable Bundle args,
    @Nullable NavOptions navOptions
)

Navigate to a destination from the current navigation graph.

@MainThread void
navigate(
    @IdRes int resId,
    @Nullable Bundle args,
    @Nullable NavOptions navOptions,
    @Nullable Navigator.Extras navigatorExtras
)

Navigate to a destination from the current navigation graph.

@MainThread void
navigate(@NonNull Uri deepLink)

Navigate to a destination via the given deep link Uri.

@MainThread void
navigate(@NonNull Uri deepLink, @Nullable NavOptions navOptions)

Navigate to a destination via the given deep link Uri.

@MainThread void
navigate(
    @NonNull Uri deepLink,
    @Nullable NavOptions navOptions,
    @Nullable Navigator.Extras navigatorExtras
)

Navigate to a destination via the given deep link Uri.

@MainThread void

Navigate to a destination via the given NavDeepLinkRequest.

@MainThread void
navigate(
    @NonNull NavDeepLinkRequest request,
    @Nullable NavOptions navOptions
)

Navigate to a destination via the given NavDeepLinkRequest.

@MainThread void
navigate(
    @NonNull NavDeepLinkRequest request,
    @Nullable NavOptions navOptions,
    @Nullable Navigator.Extras navigatorExtras
)

Navigate to a destination via the given NavDeepLinkRequest.

@MainThread void

Navigate via the given NavDirections

@MainThread void
navigate(
    @NonNull NavDirections directions,
    @Nullable NavOptions navOptions
)

Navigate via the given NavDirections

@MainThread void
navigate(
    @NonNull NavDirections directions,
    @NonNull Navigator.Extras navigatorExtras
)

Navigate via the given NavDirections

final void
navigate(
    @NonNull String route,
    @NonNull Function1<@NonNull NavOptionsBuilderUnit> builder
)

Navigate to a route in the current NavGraph.

final void
navigate(
    @NonNull String route,
    @Nullable NavOptions navOptions,
    @Nullable Navigator.Extras navigatorExtras
)

Navigate to a route in the current NavGraph.

@MainThread boolean

Attempts to navigate up in the navigation hierarchy.

@MainThread boolean

Attempts to pop the controller's back stack.

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

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

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

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

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

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

void

Removes an OnDestinationChangedListener from this controller.

@CallSuper void

Restores all navigation controller state from a bundle.

@CallSuper @Nullable Bundle

Saves all navigation controller state to a Bundle.

@MainThread @CallSuper void
setGraph(@NavigationRes int graphResId)

Sets the navigation graph to the specified resource.

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

Sets the navigation graph to the specified resource.

@MainThread @CallSuper void
setGraph(@NonNull NavGraph graph, @Nullable Bundle startDestinationArgs)

Sets the navigation graph to the specified graph.

Extension functions

final @NonNull NavGraph
NavControllerKt.createGraph(
    @NonNull NavController receiver,
    @IdRes int id,
    @IdRes int startDestination,
    @NonNull Function1<@NonNull NavGraphBuilderUnit> builder
)

This method is deprecated. Use routes to create your NavGraph instead

final @NonNull NavGraph
NavControllerKt.createGraph(
    @NonNull NavController receiver,
    @NonNull String startDestination,
    @Nullable String route,
    @NonNull Function1<@NonNull NavGraphBuilderUnit> builder
)

Construct a new NavGraph

final @NonNull NavGraph
NavControllerKt.createGraph(
    @NonNull NavController receiver,
    @IdRes int id,
    @IdRes int startDestination,
    @NonNull Function1<@NonNull DynamicNavGraphBuilderUnit> builder
)

This method is deprecated. Use routes to create your dynamic NavGraph instead

final @NonNull NavGraph
NavControllerKt.createGraph(
    @NonNull NavController receiver,
    @NonNull String startDestination,
    @Nullable String route,
    @NonNull Function1<@NonNull DynamicNavGraphBuilderUnit> builder
)

Construct a new androidx.navigation.NavGraph that supports dynamic navigation destinations

final boolean
NavControllerKt.navigateUp(
    @NonNull NavController receiver,
    @Nullable Openable drawerLayout
)

Handles the Up button by delegating its behavior to the given NavController.

final boolean
NavControllerKt.navigateUp(
    @NonNull NavController receiver,
    @NonNull AppBarConfiguration appBarConfiguration
)

Handles the Up button by delegating its behavior to the given NavController.

Public fields

currentBackStackEntry

@Nullable
public @Nullable NavBackStackEntry currentBackStackEntry

The topmost NavBackStackEntry.

Returns
@Nullable NavBackStackEntry

the topmost entry on the back stack or null if the back stack is empty

currentBackStackEntryFlow

@NonNull
public final @NonNull Flow<@NonNull NavBackStackEntrycurrentBackStackEntryFlow

A Flow that will emit the currently active NavBackStackEntry whenever it changes. If there is no active NavBackStackEntry, no item will be emitted.

currentDestination

@Nullable
public @Nullable NavDestination currentDestination

The current destination.

graph

@NonNull
public @NonNull NavGraph graph

The topmost navigation graph associated with this NavController.

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