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

Summary

Public constructors

android

Public functions

final Unit

Sets the host's LifecycleOwner.

Cmn
android
final Unit

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

Cmn
android

Inherited functions

From androidx.navigation.NavController
open Unit

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

Cmn
android
inline Boolean

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

Cmn
android
Boolean
@MainThread
<T : Any> clearBackStack(route: KClass<T>)

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

Cmn
android
Boolean
@MainThread
<T : Any> clearBackStack(route: T)

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

Cmn
android
Boolean

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

Cmn
android
inline NavBackStackEntry

Gets the topmost NavBackStackEntry for a route from KClass.

Cmn
android
NavBackStackEntry
<T : Any> getBackStackEntry(route: KClass<T>)

Gets the topmost NavBackStackEntry for a route from KClass.

Cmn
android
NavBackStackEntry
<T : Any> getBackStackEntry(route: T)

Gets the topmost NavBackStackEntry for a route from an Object.

Cmn
android
NavBackStackEntry

Gets the topmost NavBackStackEntry for a route.

Cmn
android
Boolean

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

Cmn
android
open Unit

Navigate to a destination via the given deep link Uri.

Cmn
android
open Unit

Navigate to a destination via the given NavDeepLinkRequest.

Cmn
android
open Unit
@MainThread
navigate(deepLink: NavUri, navOptions: NavOptions?)

Navigate to a destination via the given deep link Uri.

Cmn
android
open Unit

Navigate to a destination via the given NavDeepLinkRequest.

Cmn
android
Unit
@MainThread
<T : Any> navigate(route: T, builder: NavOptionsBuilder.() -> Unit)

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

Cmn
android
Unit
@MainThread
navigate(route: String, builder: NavOptionsBuilder.() -> Unit)

Navigate to a route in the current NavGraph.

Cmn
android
open Unit
@MainThread
navigate(
    deepLink: NavUri,
    navOptions: NavOptions?,
    navigatorExtras: Navigator.Extras?
)

Navigate to a destination via the given deep link Uri.

Cmn
android
open Unit
@MainThread
navigate(
    request: NavDeepLinkRequest,
    navOptions: NavOptions?,
    navigatorExtras: Navigator.Extras?
)

Navigate to a destination via the given NavDeepLinkRequest.

Cmn
android
Unit
@MainThread
<T : Any> navigate(
    route: T,
    navOptions: NavOptions?,
    navigatorExtras: Navigator.Extras?
)

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

Cmn
android
Unit
@MainThread
navigate(
    route: String,
    navOptions: NavOptions?,
    navigatorExtras: Navigator.Extras?
)

Navigate to a route in the current NavGraph.

Cmn
android
open Boolean

Attempts to navigate up in the navigation hierarchy.

Cmn
android
open Boolean

Attempts to pop the controller's back stack.

Cmn
android
inline Boolean
@MainThread
<T : Any> popBackStack(inclusive: Boolean, saveState: Boolean)

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

Cmn
android
Boolean
@MainThread
<T : Any> popBackStack(
    route: KClass<T>,
    inclusive: Boolean,
    saveState: Boolean
)

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

Cmn
android
Boolean
@MainThread
<T : Any> popBackStack(route: T, inclusive: Boolean, saveState: Boolean)

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

Cmn
android
Boolean
@MainThread
popBackStack(route: String, inclusive: Boolean, saveState: Boolean)

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

Cmn
android
open Unit

Removes an OnDestinationChangedListener from this controller.

Cmn
android
open Unit

Restores all navigation controller state from a SavedState.

Cmn
android
open SavedState?

Saves all navigation controller state to a SavedState.

Cmn
android
open Unit
@MainThread
@CallSuper
setGraph(graph: NavGraph, startDestinationArgs: SavedState?)

Sets the navigation graph to the specified graph.

Cmn
android

Inherited properties

From androidx.navigation.NavController
open NavBackStackEntry?

The topmost NavBackStackEntry.

Cmn
android
Flow<NavBackStackEntry>

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

Cmn
android
open NavDestination?

The current destination.

Cmn
android
open NavGraph

The topmost navigation graph associated with this NavController.

Cmn
android
open NavigatorProvider

The NavController's NavigatorProvider.

Cmn
android
open NavBackStackEntry?

The previous visible NavBackStackEntry.

Cmn
android
StateFlow<List<NavBackStackEntry>>

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

Cmn
android

Public constructors

NavHostController()

Public functions

setLifecycleOwner

final fun setLifecycleOwner(owner: LifecycleOwner): Unit

Sets the host's LifecycleOwner.

Parameters
owner: LifecycleOwner

The LifecycleOwner associated with the containing NavHost.

setViewModelStore

final fun setViewModelStore(viewModelStore: ViewModelStore): Unit

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