The Android Developer Challenge is back! Submit your idea before December 2.

NavHostController

public final class NavHostController
extends NavController

java.lang.Object
   ↳ 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

NavHostController(Context context)

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

Public methods

void enableOnBackPressed(boolean enabled)

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

void setLifecycleOwner(LifecycleOwner owner)

Sets the host's LifecycleOwner.

void setOnBackPressedDispatcher(OnBackPressedDispatcher dispatcher)

Sets the host's OnBackPressedDispatcher.

void setViewModelStore(ViewModelStore viewModelStore)

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

Inherited methods

Public constructors

public NavHostController (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

public void enableOnBackPressed (boolean enabled)

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

public void setLifecycleOwner (LifecycleOwner owner)

Sets the host's LifecycleOwner.

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

setOnBackPressedDispatcher

public void setOnBackPressedDispatcher (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(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.

Throws
IllegalStateException if you have not called setLifecycleOwner(LifecycleOwner) before calling this method.

setViewModelStore

public void setViewModelStore (ViewModelStore viewModelStore)

Sets the host's ViewModelStore used by the NavController to store ViewModels at the navigation graph level. This is required to call NavController.getViewModelStoreOwner(int) and should generally be called for you by your NavHost.

You must call this method before NavController.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

Throws
IllegalStateException if this method is called when graph was already set via NavController.setGraph(int) or similar methods.