NavHostController
public final class NavHostController extends 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 |
|---|
NavHostController(@NonNull Context context) |
Public methods |
|
|---|---|
final void |
enableOnBackPressed(boolean enabled)Set whether the NavController should handle the system Back button events via the registered |
final void |
setLifecycleOwner(@NonNull LifecycleOwner owner)Sets the host's |
final void |
setOnBackPressedDispatcher(@NonNull OnBackPressedDispatcher dispatcher)Sets the host's |
final void |
setViewModelStore(@NonNull ViewModelStore viewModelStore)Sets the host's ViewModelStore used by the NavController to store ViewModels at the navigation graph level. |
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
Public methods
enableOnBackPressed
public final void enableOnBackPressed(boolean enabled)
Set whether the NavController should handle the system Back button events via the registered OnBackPressedDispatcher.
| Parameters | |
|---|---|
boolean enabled |
True if the NavController should handle system Back button events. |
setLifecycleOwner
public final void setLifecycleOwner(@NonNull LifecycleOwner owner)
Sets the host's LifecycleOwner.
| Parameters | |
|---|---|
@NonNull LifecycleOwner owner |
The |
setOnBackPressedDispatcher
public final void setOnBackPressedDispatcher(@NonNull OnBackPressedDispatcher dispatcher)
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 | |
|---|---|
@NonNull OnBackPressedDispatcher dispatcher |
The |
| Throws | |
|---|---|
kotlin.IllegalStateException |
if you have not called |
| See also | |
|---|---|
setLifecycleOwner |
setViewModelStore
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 |