NavHostController

public class NavHostController extends NavController

Object
   ↳ NavController
     ↳ NavHostController
Known direct subclasses
TestNavHostController

Subclass of NavHostController that offers additional APIs for testing Navigation.


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

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.

Summary

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

final void
enableOnBackPressed(boolean enabled)

Set whether the NavController should handle the system Back button events via the registered OnBackPressedDispatcher.

final void

Sets the host's LifecycleOwner.

final void

Sets the host's OnBackPressedDispatcher.

final void

Sets the host's ViewModelStore used by the NavController to store ViewModels at the navigation graph level.

Inherited methods

From class NavController
void

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

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.

final @MainThread boolean

Clears any saved state associated with route 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(@NonNull Uri deepLink)

Navigate to a destination via the given deep link Uri.

@MainThread void

Navigate via the given NavDirections

@MainThread void

Navigate to a destination via the given NavDeepLinkRequest.

@MainThread void
navigate(@IdRes int resId)

Navigate to a destination from the current navigation graph.

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

Navigate to a destination via the given deep link Uri.

@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

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

Navigate to a destination via the given NavDeepLinkRequest.

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

Navigate to a destination from the current navigation graph.

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

Navigate to a route in the current NavGraph.

@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(
    @NonNull NavDeepLinkRequest request,
    @Nullable NavOptions navOptions,
    @Nullable Navigator.Extras navigatorExtras
)

Navigate to a destination via the given NavDeepLinkRequest.

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

Navigate to a destination from the current navigation graph.

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

Navigate to a route in the current NavGraph.

@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 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(@NonNull NavGraph graph, @Nullable Bundle startDestinationArgs)

Sets the navigation graph to the specified graph.

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

Sets the navigation graph to the specified resource.

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.

Throws
kotlin.IllegalStateException

if called before setGraph().

See also
setGraph
@NonNull
public @NonNull NavInflater navInflater

The inflater for this controller.

Returns
@NonNull NavInflater

inflater for loading navigation resources

@NonNull
public @NonNull NavigatorProvider navigatorProvider

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.

Throws
kotlin.IllegalStateException

If this set called after setGraph()

previousBackStackEntry

@Nullable
public @