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 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 NavOptionsBuilder