NavHostController


public final class NavHostController extends NavController

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.navController or by using one of the utility methods on the Navigation class.

Summary

Public constructors

Public methods

final void

Sets the host's LifecycleOwner.

final void

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

Inherited methods

From androidx.navigation.NavController
void

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

final boolean

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

final boolean

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

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

final boolean

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

final @NonNull NavBackStackEntry
<T extends Object> getBackStackEntry()

Gets the topmost NavBackStackEntry for a route from KClass.

final @NonNull NavBackStackEntry
<T extends Object> getBackStackEntry(@NonNull KClass<@NonNull T> route)

Gets the topmost NavBackStackEntry for a route from KClass.

final @NonNull NavBackStackEntry
<T extends Object> getBackStackEntry(@NonNull T route)

Gets the topmost NavBackStackEntry for a route from an Object.

final @NonNull NavBackStackEntry

Gets the topmost NavBackStackEntry for a route.

NavBackStackEntry

The topmost NavBackStackEntry.

final @NonNull Flow<@NonNull NavBackStackEntry>

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

NavDestination

The current destination.

@NonNull NavGraph

The topmost navigation graph associated with this NavController.

@NonNull NavigatorProvider

The NavController's NavigatorProvider.

NavBackStackEntry

The previous visible NavBackStackEntry.

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

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

final boolean

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

void

Navigate to a destination via the given deep link Uri.

void

Navigate to a destination via the given NavDeepLinkRequest.

void
@MainThread
navigate(@NonNull NavUri deepLink, NavOptions navOptions)

Navigate to a destination via the given deep link Uri.

void

Navigate to a destination via the given NavDeepLinkRequest.

final void
@MainThread
<T extends Object> navigate(
    @NonNull T route,
    @ExtensionFunctionType @NonNull Function1<@NonNull NavOptionsBuilderUnit> builder
)

Navigate to a route from an Object in the current NavGraph.

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

Navigate to a route in the current NavGraph.

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

Navigate to a destination via the given deep link Uri.

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

Navigate to a destination via the given NavDeepLinkRequest.

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

Navigate to a route from an Object in the current NavGraph.

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

Navigate to a route in the current NavGraph.

boolean

Attempts to navigate up in the navigation hierarchy.

boolean

Attempts to pop the controller's back stack.

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

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

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

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

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

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

void

Restores all navigation controller state from a SavedState.

SavedState

Saves all navigation controller state to a SavedState.

void

The topmost navigation graph associated with this NavController.

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

Sets the navigation graph to the specified graph.

Public constructors

public NavHostController()

Public methods

setLifecycleOwner

Added in 2.1.0
public final void setLifecycleOwner(@NonNull LifecycleOwner owner)

Sets the host's LifecycleOwner.

Parameters
@NonNull LifecycleOwner owner

The LifecycleOwner associated with the containing NavHost.

setViewModelStore

Added in 2.1.0
public final void setViewModelStore(@NonNull ViewModelStore viewModelStore)

Sets the host's ViewModelStore used by the NavController to store ViewModels at the navigation graph level. This is required to call getViewModelStoreOwner and should generally be called for you by your NavHost.

You must call this method before setGraph or similar methods, because the ViewModelStore set here will be used by the created NavBackStackEntry items.

Parameters
@NonNull ViewModelStore viewModelStore

ViewModelStore used to store ViewModels at the navigation graph level

Throws
kotlin.IllegalStateException

if this method is called when graph was already set via setGraph or similar methods.