NavHostController

class NavHostController : NavController
kotlin.Any
   ↳ androidx.navigation.NavController
   ↳ androidx.navigation.NavHostController

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

Inherited constants

Public constructors

<init>(@NonNull context: Context)

Construct a new controller for a given Context suitable for use in a NavHost.

Public methods

Unit

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

Unit

Sets the host's LifecycleOwner.

Unit

Sets the host's OnBackPressedDispatcher.

Unit
setViewModelStore(@NonNull viewModelStore: ViewModelStore)

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

Inherited functions

Public constructors

<init>

NavHostController(@NonNull context: Context)

Construct a new controller for a given Context suitable for use in a NavHost. Controllers should not be used outside of their context and retain a hard reference to the context supplied. If you need a global controller, pass Context#getApplicationContext().

Note that controllers that are not constructed with an Activity context (or a wrapped activity context) will only be able to navigate to new tasks or new document tasks when navigating to new activities.

Parameters
context Context: context for this controller

Public methods

enableOnBackPressed

fun enableOnBackPressed(enabled: Boolean): Unit

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

Parameters
enabled Boolean: True if the NavController should handle system Back button events.

setLifecycleOwner

fun setLifecycleOwner(@NonNull owner: LifecycleOwner): Unit

Sets the host's LifecycleOwner.

Parameters
owner LifecycleOwner: The LifecycleOwner associated with the containing NavHost.

setOnBackPressedDispatcher

fun setOnBackPressedDispatcher(@NonNull dispatcher: OnBackPressedDispatcher): Unit

Sets the host's OnBackPressedDispatcher. If set, NavController will register a OnBackPressedCallback to handle system Back button events.

You must explicitly called setLifecycleOwner(LifecycleOwner) before calling this method as the owner set there will be used as the LifecycleOwner for registering the OnBackPressedCallback.

You can dynamically enable and disable whether the NavController should handle the system Back button events by calling enableOnBackPressed(boolean).

Parameters
dispatcher OnBackPressedDispatcher: The OnBackPressedDispatcher associated with the containing NavHost.
Exceptions
IllegalStateException if you have not called setLifecycleOwner(LifecycleOwner) before calling this method.

setViewModelStore

fun setViewModelStore(@NonNull 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.

Parameters
viewModelStore ViewModelStore: ViewModelStore used to store ViewModels at the navigation graph level