NavController
-
Cmn
open class NavController
NavHostController |
Subclass of |
TestNavHostController |
Subclass of |
NavController manages app navigation within a NavHost.
Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly.
Navigation flows and destinations are determined by the navigation graph owned by the controller. These graphs are typically navInflater from an Android resource, but, like views, they can also be constructed or combined programmatically or for the case of dynamic navigation structure. (For example, if the navigation structure of the application is determined by live data obtained' from a remote server.)
Summary
Nested types |
|---|
fun interface NavController.OnDestinationChangedListenerOnDestinationChangedListener receives a callback when the |
Public companion functions |
||
|---|---|---|
Unit |
By default, |
Cmn
android
|
Public constructors |
|
|---|---|
NavController(context: Context) |
android
|
Public functions |
||
|---|---|---|
open Unit |
Adds an |
Cmn
android
|
inline Boolean |
@MainThreadClears any saved state associated with KClass |
Cmn
android
|
Boolean |
@MainThreadClears any saved state associated with |
android
|
Boolean |
@MainThreadClears any saved state associated with KClass |
Cmn
android
|
Boolean |
@MainThreadClears any saved state associated with KClass |
Cmn
android
|
Boolean |
@MainThreadClears any saved state associated with |
Cmn
android
|
open NavDeepLinkBuilder |
Create a deep link to a destination within this NavController. |
android
|
inline NavBackStackEntry |
<T : Any> getBackStackEntry()Gets the topmost |
Cmn
android
|
open NavBackStackEntry |
getBackStackEntry(destinationId: @IdRes Int)Gets the topmost |
android
|
NavBackStackEntry |
<T : Any> getBackStackEntry(route: KClass<T>)Gets the topmost |
Cmn
android
|
NavBackStackEntry |
<T : Any> getBackStackEntry(route: T)Gets the topmost |
Cmn
android
|
NavBackStackEntry |
getBackStackEntry(route: String)Gets the topmost |
Cmn
android
|
open ViewModelStoreOwner |
getViewModelStoreOwner(navGraphId: @IdRes Int)Gets the |
android
|
open Boolean |
@MainThreadChecks the given Intent for a Navigation deep link and navigates to the deep link if present. |
android
|
Boolean |
@MainThreadChecks the given NavDeepLinkRequest for a Navigation deep link and navigates to the destination if present. |
Cmn
android
|
open Unit |
@MainThreadNavigate to a destination via the given deep link Uri. |
Cmn
|
open Unit |
@MainThread |
android
|
open Unit |
@MainThreadNavigate via the given |
android
|
open Unit |
@MainThreadNavigate to a destination via the given |
Cmn
android
|
open Unit |
@MainThreadNavigate to a destination from the current navigation graph. |
android
|
open Unit |
@MainThreadNavigate to a destination via the given deep link Uri. |
Cmn
|
open Unit |
@MainThread |
android
|
open Unit |
@MainThreadNavigate via the given |
android
|
open Unit |
@MainThreadNavigate via the given |
android
|
open Unit |
@MainThreadNavigate to a destination via the given |
Cmn
android
|
open Unit |
@MainThreadNavigate to a destination from the current navigation graph. |
android
|
Unit |
@MainThreadNavigate to a route from an Object in the current NavGraph. |
Cmn
android
|
Unit |
@MainThreadNavigate to a route in the current NavGraph. |
Cmn
android
|
open Unit |
@MainThreadNavigate to a destination via the given deep link Uri. |
Cmn
|
open Unit |
@MainThread |
android
|
open Unit |
@MainThreadNavigate to a destination via the given |
Cmn
android
|
open Unit |
@MainThreadNavigate to a destination from the current navigation graph. |
android
|
Unit |
@MainThreadNavigate to a route from an Object in the current NavGraph. |
Cmn
android
|
Unit |
@MainThreadNavigate to a route in the current NavGraph. |
Cmn
android
|
open Unit |
@MainThreadNavigate to a destination from the current navigation graph. |
android
|
open Boolean |
Attempts to navigate up in the navigation hierarchy. |
Cmn
android
|
open Boolean |
Attempts to pop the controller's back stack. |
Cmn
android
|
open Boolean |
@MainThreadAttempts to pop the controller's back stack back to a specific destination. |
android
|
inline Boolean |
@MainThreadAttempts to pop the controller's back stack back to a specific destination. |
Cmn
android
|
open Boolean |
@MainThreadAttempts to pop the controller's back stack back to a specific destination. |
android
|
Boolean |
@MainThreadAttempts to pop the controller's back stack back to a specific destination. |
Cmn
android
|
Boolean |
@MainThreadAttempts to pop the controller's back stack back to a specific destination. |
Cmn
android
|
Boolean |
@MainThreadAttempts to pop the controller's back stack back to a specific destination. |
Cmn
android
|
open Unit |
Removes an |
Cmn
android
|
open Unit |
@CallSuper |
android
|
open Unit |
@CallSuperRestores all navigation controller state from a SavedState. |
Cmn
|
open SavedState? |
Saves all navigation controller state to a SavedState. |
Cmn
android
|
open Unit |
@MainThreadSets the |
android
|
open Unit |
@MainThread |
android
|
open Unit |
@MainThreadSets the |
Cmn
|
open Unit |
@MainThreadSets the |
android
|
Public properties |
||
|---|---|---|
open NavBackStackEntry? |
The topmost |
Cmn
android
|
Flow<NavBackStackEntry> |
A |
Cmn
android
|
open NavDestination? |
The current destination. |
Cmn
android
|
open NavGraph |
The topmost navigation graph associated with this NavController. |
Cmn
android
|
open NavInflater |
The |
android
|
open NavigatorProvider |
The NavController's |
Cmn
android
|
open NavBackStackEntry? |
The previous visible |
Cmn
android
|
StateFlow<List<NavBackStackEntry>> |
A |
Cmn
android
|
Extension functions |
||
|---|---|---|
inline NavGraph |
NavController.This function is deprecated. Use routes to create your NavGraph instead |
android
|
inline NavGraph |
NavController.This function is deprecated. Use routes to create your dynamic NavGraph instead |
android
|
inline NavGraph |
NavController.createGraph(Construct a new |
Cmn
|
inline NavGraph |
NavController.createGraph(Construct a new |
android
|
inline NavGraph |
NavController.createGraph(Construct a new |
Cmn
|
inline NavGraph |
NavController.createGraph(Construct a new |
android
|
inline NavGraph |
NavController.createGraph(Construct a new |
Cmn
|
inline NavGraph |
NavController.createGraph(Construct a new |
android
|
Boolean |
NavController.navigateUp(appBarConfiguration: AppBarConfiguration)Handles the Up button by delegating its behavior to the given |
android
|
Boolean |
NavController.navigateUp(drawerLayout: Openable?)Handles the Up button by delegating its behavior to the given |
android
|
State<NavBackStackEntry?> |
Gets the current navigation back stack entry as a |
Cmn
|
State<NavBackStackEntry?> |
Gets the current navigation back stack entry as a |
android
|
Constants
KEY_DEEP_LINK_INTENT
const val KEY_DEEP_LINK_INTENT: String
The Intent that triggered a deep link to the current destination.
Public companion functions
enableDeepLinkSaveState
@NavDeepLinkSaveStateControl
fun enableDeepLinkSaveState(saveState: Boolean): Unit
By default, handleDeepLink will automatically add calls to NavOptions.Builder.setPopUpTo with a saveState of true when the deep link takes you to another graph (e.g., a different navigation graph than the one your start destination is in).
You can disable this behavior by passing false for saveState.
Public constructors
Public functions
addOnDestinationChangedListener
open fun addOnDestinationChangedListener(
listener: NavController.OnDestinationChangedListener
): Unit
Adds an OnDestinationChangedListener to this controller to receive a callback whenever the currentDestination or its arguments change.
The current destination, if any, will be immediately sent to your listener.
| Parameters | |
|---|---|
listener: NavController.OnDestinationChangedListener |
the listener to receive events |
clearBackStack
@MainThread
inline fun <T : Any> clearBackStack(): Boolean
Clears any saved state associated with KClass T that was previously saved via popBackStack when using a saveState value of true.
| Parameters | |
|---|---|
<T : Any> |
The route from the |
clearBackStack
@MainThread
fun clearBackStack(destinationId: @IdRes Int): Boolean
Clears any saved state associated with destinationId that was previously saved via popBackStack when using a saveState value of true.
| Parameters | |
|---|---|
destinationId: @IdRes Int |
The ID of the destination previously used with |
| Returns | |
|---|---|
Boolean |
true if the saved state of the stack associated with |
clearBackStack
@MainThread
fun <T : Any> clearBackStack(route: KClass<T>): Boolean
Clears any saved state associated with KClass route that was previously saved via popBackStack when using a saveState value of true.
| Parameters | |
|---|---|
route: KClass<T> |
The route from the |
clearBackStack
@MainThread
fun <T : Any> clearBackStack(route: T): Boolean
Clears any saved state associated with KClass T that was previously saved via popBackStack when using a saveState value of true.
| Parameters | |
|---|---|
route: T |
The route from an Object of the destination previously used with |
clearBackStack
@MainThread
fun clearBackStack(route: String): Boolean
Clears any saved state associated with route that was previously saved via popBackStack when using a saveState value of true.
| Parameters | |
|---|---|
route: String |
The route of the destination previously used with |
createDeepLink
open fun createDeepLink(): NavDeepLinkBuilder
Create a deep link to a destination within this NavController.
| Returns | |
|---|---|
NavDeepLinkBuilder |
a |
getBackStackEntry
inline fun <T : Any> getBackStackEntry(): NavBackStackEntry
Gets the topmost NavBackStackEntry for a route from KClass.
This is always safe to use with the current destination or its parent or grandparent navigation graphs as these destinations are guaranteed to be on the back stack.
| Parameters | |
|---|---|
<T : Any> |
route from the |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the destination is not on the back stack |
getBackStackEntry
open fun getBackStackEntry(destinationId: @IdRes Int): NavBackStackEntry
Gets the topmost NavBackStackEntry for a destination id.
This is always safe to use with the current destination or its parent or grandparent navigation graphs as these destinations are guaranteed to be on the back stack.
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the destination is not on the back stack |
getBackStackEntry
fun <T : Any> getBackStackEntry(route: KClass<T>): NavBackStackEntry
Gets the topmost NavBackStackEntry for a route from KClass.
This is always safe to use with the current destination or its parent or grandparent navigation graphs as these destinations are guaranteed to be on the back stack.
| Parameters | |
|---|---|
route: KClass<T> |
route from the |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the destination is not on the back stack |
getBackStackEntry
fun <T : Any> getBackStackEntry(route: T): NavBackStackEntry
Gets the topmost NavBackStackEntry for a route from an Object.
This is always safe to use with the current destination or its parent or grandparent navigation graphs as these destinations are guaranteed to be on the back stack.
| Parameters | |
|---|---|
route: T |
route from an Object of a destination that exists on the back stack. The target NavBackStackEntry's |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the destination is not on the back stack |
getBackStackEntry
fun getBackStackEntry(route: String): NavBackStackEntry
Gets the topmost NavBackStackEntry for a route.
This is always safe to use with the current destination or its parent or grandparent navigation graphs as these destinations are guaranteed to be on the back stack.
| Parameters | |
|---|---|
route: String |
route of a destination that exists on the back stack. May contain filled in arguments as long as it is exact match with route used to navigate. |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the destination is not on the back stack |
getViewModelStoreOwner
open fun getViewModelStoreOwner(navGraphId: @IdRes Int): ViewModelStoreOwner
Gets the ViewModelStoreOwner for a NavGraph. This can be passed to androidx.lifecycle.ViewModelProvider to retrieve a ViewModel that is scoped to the navigation graph - it will be cleared when the navigation graph is popped off the back stack.
| Throws | |
|---|---|
kotlin.IllegalStateException |
if called before the |
kotlin.IllegalArgumentException |
if the NavGraph is not on the back stack |
handleDeepLink
@MainThread
open fun handleDeepLink(intent: Intent?): Boolean
Checks the given Intent for a Navigation deep link and navigates to the deep link if present. This is called automatically for you the first time you set the graph if you've passed in an Activity as the context when constructing this NavController, but should be manually called if your Activity receives new Intents in Activity.onNewIntent.
The types of Intents that are supported include:
Intents created by NavDeepLinkBuilder or createDeepLink. This assumes that the current graph shares the same hierarchy to get to the deep linked destination as when the deep link was constructed. Intents that include a data Uri. This Uri will be checked against the Uri patterns in the NavDeepLinks added via NavDestination.addDeepLink.
The navigation graph should be set before calling this method.
| Parameters | |
|---|---|
intent: Intent? |
The Intent that may contain a valid deep link |
| Returns | |
|---|---|
Boolean |
True if the navigation controller found a valid deep link and navigated to it. |
| Throws | |
|---|---|
kotlin.IllegalStateException |
if deep link cannot be accessed from the current destination |
| See also | |
|---|---|
addDeepLink |
handleDeepLink
@MainThread
fun handleDeepLink(request: NavDeepLinkRequest): Boolean
Checks the given NavDeepLinkRequest for a Navigation deep link and navigates to the destination if present.
The navigation graph should be set before calling this method.
| Parameters | |
|---|---|
request: NavDeepLinkRequest |
The request that contains a valid deep link, an action or a mimeType. |
| Returns | |
|---|---|
Boolean |
True if the navigation controller found a valid deep link and navigated to it. |
| Throws | |
|---|---|
kotlin.IllegalStateException |
if deep link cannot be accessed from the current destination |
| See also | |
|---|---|
addDeepLink |
navigate
@MainThread
open fun navigate(deepLink: NavUri): Unit
Navigate to a destination via the given deep link Uri. NavDestination.hasDeepLink should be called on the navigation graph prior to calling this method to check if the deep link is valid. If an invalid deep link is given, an IllegalArgumentException will be thrown.
| Parameters | |
|---|---|
deepLink: NavUri |
deepLink to the destination reachable from the current NavGraph |
| See also | |
|---|---|
navigate |
navigate
@MainThread
open fun navigate(directions: NavDirections): Unit
Navigate via the given NavDirections
| Parameters | |
|---|---|
directions: NavDirections |
directions that describe this navigation operation |
navigate
@MainThread
open fun navigate(request: NavDeepLinkRequest): Unit
Navigate to a destination via the given NavDeepLinkRequest. NavDestination.hasDeepLink should be called on the navigation graph prior to calling this method to check if the deep link is valid. If an invalid deep link is given, an IllegalArgumentException will be thrown.
| Parameters | |
|---|---|
request: NavDeepLinkRequest |
deepLinkRequest to the destination reachable from the current NavGraph |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given deep link request is invalid |
navigate
@MainThread
open fun navigate(resId: @IdRes Int): Unit
Navigate to a destination from the current navigation graph. This supports both navigating via an action and directly navigating to a destination.
| Throws | |
|---|---|
kotlin.IllegalStateException |
if there is no current navigation node |
kotlin.IllegalArgumentException |
if the desired destination cannot be found from the current destination |
navigate
@MainThread
open fun navigate(deepLink: NavUri, navOptions: NavOptions?): Unit
Navigate to a destination via the given deep link Uri. NavDestination.hasDeepLink should be called on the navigation graph prior to calling this method to check if the deep link is valid. If an invalid deep link is given, an IllegalArgumentException will be thrown.
| Parameters | |
|---|---|
deepLink: NavUri |
deepLink to the destination reachable from the current NavGraph |
navOptions: NavOptions? |
special options for this navigation operation |
| See also | |
|---|---|
navigate |
navigate
@MainThread
open fun navigate(directions: NavDirections, navOptions: NavOptions?): Unit
Navigate via the given NavDirections
| Parameters | |
|---|---|
directions: NavDirections |
directions that describe this navigation operation |
navOptions: NavOptions? |
special options for this navigation operation |
navigate
@MainThread
open fun navigate(directions: NavDirections, navigatorExtras: Navigator.Extras): Unit
Navigate via the given NavDirections
| Parameters | |
|---|---|
directions: NavDirections |
directions that describe this navigation operation |
navigatorExtras: Navigator.Extras |
extras to pass to the |
navigate
@MainThread
open fun navigate(request: NavDeepLinkRequest, navOptions: NavOptions?): Unit
Navigate to a destination via the given NavDeepLinkRequest. NavDestination.hasDeepLink should be called on the navigation graph prior to calling this method to check if the deep link is valid. If an invalid deep link is given, an IllegalArgumentException will be thrown.
| Parameters | |
|---|---|
request: NavDeepLinkRequest |
deepLinkRequest to the destination reachable from the current NavGraph |
navOptions: NavOptions? |
special options for this navigation operation |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given deep link request is invalid |
navigate
@MainThread
open fun navigate(resId: @IdRes Int, args: Bundle?): Unit
Navigate to a destination from the current navigation graph. This supports both navigating via an action and directly navigating to a destination.
| Parameters | |
|---|---|
resId: @IdRes Int |
an |
args: Bundle? |
arguments to pass to the destination |
| Throws | |
|---|---|
kotlin.IllegalStateException |
if there is no current navigation node |
kotlin.IllegalArgumentException |
if the desired destination cannot be found from the current destination |
navigate
@MainThread
fun <T : Any> navigate(route: T, builder: NavOptionsBuilder.() -> Unit): Unit
Navigate to a route from an Object in the current NavGraph. If an invalid route is given, an IllegalArgumentException will be thrown.
The target NavDestination must have been created with route from a KClass
If given NavOptions pass in NavOptions.restoreState true, any args passed here as part of the route will be overridden by the restored args.
| Parameters | |
|---|---|
route: T |
route from an Object for the destination |
builder: NavOptionsBuilder.() -> Unit |
DSL for constructing a new |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given route is invalid |
navigate
@MainThread
fun navigate(route: String, builder: NavOptionsBuilder.() -> Unit): Unit
Navigate to a route in the current NavGraph. If an invalid route is given, an IllegalArgumentException will be thrown.
If given NavOptions pass in NavOptions.restoreState true, any args passed here as part of the route will be overridden by the restored args.
| Parameters | |
|---|---|
route: String |
route for the destination |
builder: NavOptionsBuilder.() -> Unit |
DSL for constructing a new |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given route is invalid |
navigate
@MainThread
open fun navigate(
deepLink: NavUri,
navOptions: NavOptions?,
navigatorExtras: Navigator.Extras?
): Unit
Navigate to a destination via the given deep link Uri. NavDestination.hasDeepLink should be called on the navigation graph prior to calling this method to check if the deep link is valid. If an invalid deep link is given, an IllegalArgumentException will be thrown.
| Parameters | |
|---|---|
deepLink: NavUri |
deepLink to the destination reachable from the current NavGraph |
navOptions: NavOptions? |
special options for this navigation operation |
navigatorExtras: Navigator.Extras? |
extras to pass to the Navigator |
| See also | |
|---|---|
navigate |
navigate
@MainThread
open fun navigate(
deepLink: Uri,
navOptions: NavOptions?,
navigatorExtras: Navigator.Extras?
): Unit
navigate
@MainThread
open fun navigate(
request: NavDeepLinkRequest,
navOptions: NavOptions?,
navigatorExtras: Navigator.Extras?
): Unit
Navigate to a destination via the given NavDeepLinkRequest. NavDestination.hasDeepLink should be called on the navigation graph prior to calling this method to check if the deep link is valid. If an invalid deep link is given, an IllegalArgumentException will be thrown.
| Parameters | |
|---|---|
request: NavDeepLinkRequest |
deepLinkRequest to the destination reachable from the current NavGraph |
navOptions: NavOptions? |
special options for this navigation operation |
navigatorExtras: Navigator.Extras? |
extras to pass to the Navigator |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given deep link request is invalid |
navigate
@MainThread
open fun navigate(resId: @IdRes Int, args: Bundle?, navOptions: NavOptions?): Unit
Navigate to a destination from the current navigation graph. This supports both navigating via an action and directly navigating to a destination.
If given NavOptions pass in NavOptions.restoreState true, any args passed here will be overridden by the restored args.
| Parameters | |
|---|---|
resId: @IdRes Int |
an |
args: Bundle? |
arguments to pass to the destination |
navOptions: NavOptions? |
special options for this navigation operation |
| Throws | |
|---|---|
kotlin.IllegalStateException |
if there is no current navigation node |
kotlin.IllegalArgumentException |
if the desired destination cannot be found from the current destination |
navigate
@MainThread
fun <T : Any> navigate(
route: T,
navOptions: NavOptions? = null,
navigatorExtras: Navigator.Extras? = null
): Unit
Navigate to a route from an Object in the current NavGraph. If an invalid route is given, an IllegalArgumentException will be thrown.
The target NavDestination must have been created with route from a KClass
If given NavOptions pass in NavOptions.restoreState true, any args passed here as part of the route will be overridden by the restored args.
| Parameters | |
|---|---|
route: T |
route from an Object for the destination |
navOptions: NavOptions? = null |
special options for this navigation operation |
navigatorExtras: Navigator.Extras? = null |
extras to pass to the |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given route is invalid |
navigate
@MainThread
fun navigate(
route: String,
navOptions: NavOptions? = null,
navigatorExtras: Navigator.Extras? = null
): Unit
Navigate to a route in the current NavGraph. If an invalid route is given, an IllegalArgumentException will be thrown.
If given NavOptions pass in NavOptions.restoreState true, any args passed here as part of the route will be overridden by the restored args.
| Parameters | |
|---|---|
route: String |
route for the destination |
navOptions: NavOptions? = null |
special options for this navigation operation |
navigatorExtras: Navigator.Extras? = null |
extras to pass to the |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given route is invalid |
navigate
@MainThread
open fun navigate(
resId: @IdRes Int,
args: Bundle?,
navOptions: NavOptions?,
navigatorExtras: Navigator.Extras?
): Unit
Navigate to a destination from the current navigation graph. This supports both navigating via an action and directly navigating to a destination.
If given NavOptions pass in NavOptions.restoreState true, any args passed here will be overridden by the restored args.
| Parameters | |
|---|---|
resId: @IdRes Int |
an |
args: Bundle? |
arguments to pass to the destination |
navOptions: NavOptions? |
special options for this navigation operation |
navigatorExtras: Navigator.Extras? |
extras to pass to the Navigator |
| Throws | |
|---|---|
kotlin.IllegalStateException |
if navigation graph has not been set for this NavController |
kotlin.IllegalArgumentException |
if the desired destination cannot be found from the current destination |
navigateUp
@MainThread
open fun navigateUp(): Boolean
Attempts to navigate up in the navigation hierarchy. Suitable for when the user presses the "Up" button marked with a left (or start)-facing arrow in the upper left (or starting) corner of the app UI.
The intended behavior of Up differs from Back when the user did not reach the current destination from the application's own task. e.g. if the user is viewing a document or link in the current app in an activity hosted on another app's task where the user clicked the link. In this case the current activity (determined by the context used to create this NavController) will be Activity.finish and the user will be taken to an appropriate destination in this app on its own task.
| Returns | |
|---|---|
Boolean |
true if navigation was successful, false otherwise |
popBackStack
@MainThread
open fun popBackStack(): Boolean
Attempts to pop the controller's back stack. Analogous to when the user presses the system android.view.KeyEvent.KEYCODE_BACK button when the associated navigation host has focus.
| Returns | |
|---|---|
Boolean |
true if the stack was popped at least once and the user has been navigated to another destination, false otherwise |
popBackStack
@MainThread
open fun popBackStack(destinationId: @IdRes Int, inclusive: Boolean): Boolean
Attempts to pop the controller's back stack back to a specific destination.
| Parameters | |
|---|---|
destinationId: @IdRes Int |
The topmost destination to retain |
inclusive: Boolean |
Whether the given destination should also be popped. |
| Returns | |
|---|---|
Boolean |
true if the stack was popped at least once and the user has been navigated to another destination, false otherwise |
popBackStack
@MainThread
inline fun <T : Any> popBackStack(inclusive: Boolean, saveState: Boolean = false): Boolean
Attempts to pop the controller's back stack back to a specific destination.
| Parameters | |
|---|---|
<T : Any> |
The topmost destination to retain with route from a |
inclusive: Boolean |
Whether the given destination should also be popped. |
saveState: Boolean = false |
Whether the back stack and the state of all destinations between the current destination and |
| Returns | |
|---|---|
Boolean |
true if the stack was popped at least once and the user has been navigated to another destination, false otherwise |
popBackStack
@MainThread
open fun popBackStack(
destinationId: @IdRes Int,
inclusive: Boolean,
saveState: Boolean
): Boolean
Attempts to pop the controller's back stack back to a specific destination.
| Parameters | |
|---|---|
destinationId: @IdRes Int |
The topmost destination to retain |
inclusive: Boolean |
Whether the given destination should also be popped. |
saveState: Boolean |
Whether the back stack and the state of all destinations between the current destination and the |
| Returns | |
|---|---|
Boolean |
true if the stack was popped at least once and the user has been navigated to another destination, false otherwise |
popBackStack
@MainThread
fun <T : Any> popBackStack(
route: KClass<T>,
inclusive: Boolean,
saveState: Boolean = false
): Boolean
Attempts to pop the controller's back stack back to a specific destination.
| Parameters | |
|---|---|
route: KClass<T> |
The topmost destination to retain with route from a |
inclusive: Boolean |
Whether the given destination should also be popped. |
saveState: Boolean = false |
Whether the back stack and the state of all destinations between the current destination and |
| Returns | |
|---|---|
Boolean |
true if the stack was popped at least once and the user has been navigated to another destination, false otherwise |
popBackStack
@MainThread
fun <T : Any> popBackStack(route: T, inclusive: Boolean, saveState: Boolean = false): Boolean
Attempts to pop the controller's back stack back to a specific destination.
| Parameters | |
|---|---|
route: T |
The topmost destination to retain with route from an Object. The target NavDestination must have been created with route from |
inclusive: Boolean |
Whether the given destination should also be popped. |
saveState: Boolean = false |
Whether the back stack and the state of all destinations between the current destination and the |
| Returns | |
|---|---|
Boolean |
true if the stack was popped at least once and the user has been navigated to another destination, false otherwise |
popBackStack
@MainThread
fun popBackStack(route: String, inclusive: Boolean, saveState: Boolean = false): Boolean
Attempts to pop the controller's back stack back to a specific destination.
| Parameters | |
|---|---|
route: String |
The topmost destination to retain. May contain filled in arguments as long as it is exact match with route used to navigate. |
inclusive: Boolean |
Whether the given destination should also be popped. |
saveState: Boolean = false |
Whether the back stack and the state of all destinations between the current destination and the |
| Returns | |
|---|---|
Boolean |
true if the stack was popped at least once and the user has been navigated to another destination, false otherwise |
removeOnDestinationChangedListener
open fun removeOnDestinationChangedListener(
listener: NavController.OnDestinationChangedListener
): Unit
Removes an OnDestinationChangedListener from this controller. It will no longer receive callbacks.
| Parameters | |
|---|---|
listener: NavController.OnDestinationChangedListener |
the listener to remove |
restoreState
@CallSuper
open fun restoreState(navState: SavedState?): Unit
Restores all navigation controller state from a SavedState. This should be called before any call to setGraph.
State may be saved to a SavedState by calling saveState. Restoring controller state is the responsibility of a NavHost.
| Parameters | |
|---|---|
navState: SavedState? |
SavedState to restore |
saveState
@CallSuper
open fun saveState(): SavedState?
Saves all navigation controller state to a SavedState.
State may be restored from a SavedState returned from this method by calling restoreState. Saving controller state is the responsibility of a NavHost.
| Returns | |
|---|---|
SavedState? |
saved state for this controller |
setGraph
@MainThread
@CallSuper
open fun setGraph(graphResId: @NavigationRes Int): Unit
Sets the navigation graph to the specified resource. Any current navigation graph data (including back stack) will be replaced.
The inflated graph can be retrieved via graph.
| Parameters | |
|---|---|
graphResId: @NavigationRes Int |
resource id of the navigation graph to inflate |
| See also | |
|---|---|
navInflater |
|
setGraph |
|
graph |
setGraph
@MainThread
@CallSuper
open fun setGraph(graph: NavGraph, startDestinationArgs: Bundle?): Unit
setGraph
@MainThread
@CallSuper
open fun setGraph(graph: NavGraph, startDestinationArgs: SavedState?): Unit
Sets the navigation graph to the specified graph. Any current navigation graph data (including back stack) will be replaced.
The graph can be retrieved later via graph.
| Parameters | |
|---|---|
graph: NavGraph |
graph to set |
startDestinationArgs: SavedState? |
arguments to send to the start destination of the graph |
setGraph
@MainThread
@CallSuper
open fun setGraph(graphResId: @NavigationRes Int, startDestinationArgs: Bundle?): Unit
Sets the navigation graph to the specified resource. Any current navigation graph data (including back stack) will be replaced.
The inflated graph can be retrieved via graph.
| Parameters | |
|---|---|
graphResId: @NavigationRes Int |
resource id of the navigation graph to inflate |
startDestinationArgs: Bundle? |
arguments to send to the start destination of the graph |
| See also | |
|---|---|
navInflater |
|
setGraph |
|
graph |
Public properties
currentBackStackEntry
open val currentBackStackEntry: NavBackStackEntry?
The topmost NavBackStackEntry.
| Returns | |
|---|---|
NavBackStackEntry? |
the topmost entry on the back stack or null if the back stack is empty |
currentBackStackEntryFlow
val currentBackStackEntryFlow: Flow<NavBackStackEntry>
A Flow that will emit the currently active NavBackStackEntry whenever it changes. If there is no active NavBackStackEntry, no item will be emitted.
graph
open var graph: NavGraph
The topmost navigation graph associated with this NavController.
When this is set any current navigation graph data (including back stack) will be replaced.
| Throws | |
|---|---|
kotlin.IllegalStateException |
if called before |
| See also | |
|---|---|
setGraph |
navInflater
open val navInflater: NavInflater
The inflater for this controller.
| Returns | |
|---|---|
NavInflater |
inflater for loading navigation resources |
navigatorProvider
open val navigatorProvider: NavigatorProvider
The NavController's NavigatorProvider. All Navigators used to construct the navigation graph for this nav controller should be added to this navigator provider before the graph is constructed.
This can only be set before the graph is set via setGraph().
Generally, the Navigators are set for you by the NavHost hosting this NavController and you do not need to manually interact with the navigator provider.
| Throws | |
|---|---|
kotlin.IllegalStateException |
If this set called after |
previousBackStackEntry
open val previousBackStackEntry: NavBackStackEntry?
The previous visible NavBackStackEntry.
This skips over any NavBackStackEntry that is associated with a NavGraph.
| Returns | |
|---|---|
NavBackStackEntry? |
the previous visible entry on the back stack or null if the back stack has less than two visible entries |
visibleEntries
val visibleEntries: StateFlow<List<NavBackStackEntry>>
A StateFlow that will emit the currently visible NavBackStackEntries whenever they change. If there is no visible NavBackStackEntry, this will be set to an empty list.
-
CREATEDentries are listed first and include all entries that are in the process of completing their exit transition. Note that this can include entries that have been popped off the Navigation back stack. -
STARTEDentries on the back stack are next and include all entries that are running their enter transition and entries whose destination is partially covered by aFloatingWindowdestination -
The last entry in the list is the topmost entry in the back stack and is in the
RESUMEDstate only if its enter transition has completed. Otherwise it too will beSTARTED.
Note that the Lifecycle of any entry cannot be higher than the containing Activity/Fragment - if the Activity is not RESUMED, no entry will be RESUMED, no matter what the transition state is.
Extension functions
createGraph
inline fun NavController.createGraph(
id: @IdRes Int = 0,
startDestination: @IdRes Int,
builder: NavGraphBuilder.() -> Unit
): NavGraph
Construct a new NavGraph
createGraph
inline fun NavController.createGraph(
id: @IdRes Int = 0,
startDestination: @IdRes Int,
builder: DynamicNavGraphBuilder.() -> Unit
): NavGraph
Construct a new androidx.navigation.NavGraph that supports dynamic navigation destinations
createGraph
inline fun NavController.createGraph(
startDestination: String,
route: String? = null,
builder: NavGraphBuilder.() -> Unit
): NavGraph
Construct a new NavGraph
| Parameters | |
|---|---|
startDestination: String |
the route for the start destination |
route: String? = null |
the route for the graph |
builder: NavGraphBuilder.() -> Unit |
the builder used to construct the graph |
createGraph
inline fun NavController.createGraph(
startDestination: String,
route: String? = null,
builder: DynamicNavGraphBuilder.() -> Unit
): NavGraph
Construct a new androidx.navigation.NavGraph that supports dynamic navigation destinations
createGraph
inline fun NavController.createGraph(
startDestination: Any,
route: KClass<*>? = null,
typeMap: Map<KType, NavType<*>> = emptyMap(),
builder: NavGraphBuilder.() -> Unit
): NavGraph
Construct a new NavGraph
| Parameters | |
|---|---|
startDestination: Any |
the starting destination's route from an Object for this NavGraph. The respective NavDestination must be added as a |
route: KClass<*>? = null |
the graph's unique route from a |
typeMap: Map<KType, NavType<*>> = emptyMap() |
A mapping of KType to custom NavType<*> in the |
builder: NavGraphBuilder.() -> Unit |
the builder used to construct the graph |
createGraph
inline fun NavController.createGraph(
startDestination: Any,
route: KClass<*>? = null,
typeMap: Map<KType, NavType<*>> = emptyMap(),
builder: DynamicNavGraphBuilder.() -> Unit
): NavGraph
Construct a new androidx.navigation.NavGraph that supports dynamic navigation destinations
| Parameters | |
|---|---|
startDestination: Any |
the starting destination's route from an Object for this NavGraph. The respective NavDestination must be added as a |
route: KClass<*>? = null |
the graph's unique route from a |
typeMap: Map<KType, NavType<*>> = emptyMap() |
A mapping of KType to custom NavType<*> in the |
builder: DynamicNavGraphBuilder.() -> Unit |
the builder used to construct the graph |
createGraph
inline fun NavController.createGraph(
startDestination: KClass<*>,
route: KClass<*>? = null,
typeMap: Map<KType, NavType<*>> = emptyMap(),
builder: NavGraphBuilder.() -> Unit
): NavGraph
Construct a new NavGraph
| Parameters | |
|---|---|
startDestination: KClass<*> |
the starting destination's route from a |
route: KClass<*>? = null |
the graph's unique route from a |
typeMap: Map<KType, NavType<*>> = emptyMap() |
A mapping of KType to custom NavType<*> in the |
builder: NavGraphBuilder.() -> Unit |
the builder used to construct the graph |
createGraph
inline fun NavController.createGraph(
startDestination: KClass<*>,
route: KClass<*>? = null,
typeMap: Map<KType, NavType<*>> = emptyMap(),
builder: DynamicNavGraphBuilder.() -> Unit
): NavGraph
Construct a new androidx.navigation.NavGraph that supports dynamic navigation destinations
| Parameters | |
|---|---|
startDestination: KClass<*> |
the starting destination's route from a |
route: KClass<*>? = null |
the graph's unique route from a |
typeMap: Map<KType, NavType<*>> = emptyMap() |
A mapping of KType to custom NavType<*> in the |
builder: DynamicNavGraphBuilder.() -> Unit |
the builder used to construct the graph |
navigateUp
fun NavController.navigateUp(appBarConfiguration: AppBarConfiguration): Boolean
Handles the Up button by delegating its behavior to the given NavController.
| Returns | |
|---|---|
Boolean |
True if the |
navigateUp
fun NavController.navigateUp(drawerLayout: Openable?): Boolean
Handles the Up button by delegating its behavior to the given NavController.
This is equivalent to calling NavController.navigateUp if the Openable layout is null.
| Returns | |
|---|---|
Boolean |
True if the |
currentBackStackEntryAsState
@Composable
fun NavController.currentBackStackEntryAsState(): State<NavBackStackEntry?>
Gets the current navigation back stack entry as a MutableState. When the given navController changes the back stack due to a NavController.navigate or NavController.popBackStack this will trigger a recompose and return the top entry on the back stack.
| Returns | |
|---|---|
State<NavBackStackEntry?> |
a mutable state of the current back stack entry |
currentBackStackEntryAsState
@Composable
fun NavController.currentBackStackEntryAsState(): State<NavBackStackEntry?>
Gets the current navigation back stack entry as a State. When the given navController changes the back stack due to a NavController.navigate or NavController.popBackStack this will trigger a recompose and return the top entry on the back stack.
| Returns | |
|---|---|
State<NavBackStackEntry?> |
state of the current back stack entry |