NavHostController
-
Cmn
open class NavHostController : NavController
TestNavHostController |
Subclass of |
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
|
|
NavHostController(context: Context) |
android
|
Public functions |
||
|---|---|---|
final Unit |
enableOnBackPressed(enabled: Boolean)Set whether the NavController should handle the system Back button events via the registered |
android
|
final Unit |
setLifecycleOwner(owner: LifecycleOwner)Sets the host's |
Cmn
android
|
final Unit |
setOnBackPressedDispatcher(dispatcher: OnBackPressedDispatcher)Sets the host's |
android
|
final Unit |
setViewModelStore(viewModelStore: ViewModelStore)Sets the host's ViewModelStore used by the NavController to store ViewModels at the navigation graph level. |
Cmn
android
|
Inherited functions |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Inherited properties |
||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
NavHostController
NavHostController(context: Context)
| Parameters | |
|---|---|
context: Context |
context for this controller |
Public functions
enableOnBackPressed
final 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
final fun setLifecycleOwner(owner: LifecycleOwner): Unit
Sets the host's LifecycleOwner.
| Parameters | |
|---|---|
owner: LifecycleOwner |
The |
setOnBackPressedDispatcher
final fun setOnBackPressedDispatcher(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 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.
| Parameters | |
|---|---|
dispatcher: OnBackPressedDispatcher |
The |
| Throws | |
|---|---|
IllegalStateException |
if you have not called |
| See also | |
|---|---|
setLifecycleOwner |
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 | |
|---|---|
IllegalStateException |
if this method is called when graph was already set via |