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 |
|
---|---|
Construct a new controller for a given |
Public methods |
|
---|---|
Unit |
Set whether the NavController should handle the system Back button events via the registered |
Unit |
setLifecycleOwner(@NonNull : LifecycleOwner) Sets the host's |
Unit |
setOnBackPressedDispatcher(@NonNull : OnBackPressedDispatcher) Sets the host's |
Unit |
setViewModelStore(@NonNull : 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)
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(: 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 : LifecycleOwner): Unit
Sets the host's LifecycleOwner
.
Parameters | |
---|---|
owner |
LifecycleOwner: The LifecycleOwner associated with the containing NavHost . |
setOnBackPressedDispatcher
fun setOnBackPressedDispatcher(@NonNull : 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. |
See Also
setViewModelStore
fun setViewModelStore(@NonNull : 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(int)
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 |
Exceptions | |
---|---|
IllegalStateException |
if this method is called when graph was already set via setGraph(int) or similar methods. |