open class NavGraph : NavDestination, Iterable


NavGraph is a collection of NavDestination nodes fetchable by ID.

A NavGraph serves as a 'virtual' destination: while the NavGraph itself will not appear on the back stack, navigating to the NavGraph will cause the starting destination to be added to the back stack.

Construct a new NavGraph. This NavGraph is not valid until you add a destination and set the starting destination.

Summary

Public companion functions

NavDestination

Finds the actual start destination of the graph, handling cases where the graph's starting destination is itself a NavGraph.

Public constructors

NavGraph(navGraphNavigator: Navigator<NavGraph>)

Public functions

Unit
addAll(other: NavGraph)

Add all destinations from another collection to this one.

Unit

Adds a destination to this NavGraph.

Unit

Adds multiple destinations to this NavGraph.

Unit

Adds multiple destinations to this NavGraph.

Unit

Clear all destinations from this navigation graph.

open operator Boolean
equals(other: Any?)
inline NavDestination?
<T : Any?> findNode()

Finds a destination in the collection by route from KClass.

NavDestination?
findNode(resId: @IdRes Int)

Finds a destination in the collection by ID.

NavDestination?
findNode(route: KClass<*>)

Finds a destination in the collection by route from KClass.

NavDestination?
<T : Any?> findNode(route: T?)

Finds a destination in the collection by route from Object.

NavDestination?
findNode(route: String?)

Finds a destination in the collection by route.

@IdRes Int

This function is deprecated. Use getStartDestinationId instead.

open Int
final operator MutableIterator<NavDestination>
open Unit
onInflate(context: Context, attrs: AttributeSet)

Called when inflating a destination from a resource.

Unit

Remove a given destination from this NavGraph

inline Unit

Sets the starting destination for this NavGraph.

Unit
setStartDestination(startDestId: Int)

Sets the starting destination for this NavGraph.

Unit
<T : Any> setStartDestination(startDestRoute: KClass<T>)

Sets the starting destination for this NavGraph.

Unit
<T : Any> setStartDestination(startDestRoute: T)

Sets the starting destination for this NavGraph.

Unit
setStartDestination(startDestRoute: String)

Sets the starting destination for this NavGraph.

open String

Public properties

Int

The starting destination id for this NavGraph.

String?

The route for the starting destination for this NavGraph.

Extension functions

operator Boolean

Returns true if a destination with id is found in this navigation graph.

inline operator Boolean
<T : Any> NavGraph.contains(route: KClass<T>)

Returns true if a destination with route is found in this navigation graph.

operator Boolean

Returns true if a destination with route is found in this navigation graph.

operator Boolean
<T : Any> NavGraph.contains(route: T)

Returns true if a destination with route is found in this navigation graph.

inline operator NavDestination

Returns the destination with id.

inline operator NavDestination
<T : Any> NavGraph.get(route: KClass<T>)

Returns the destination with route from KClass.

inline operator NavDestination

Returns the destination with route.

inline operator NavDestination
<T : Any> NavGraph.get(route: T)

Returns the destination with route from an Object.

inline operator Unit

Removes node from this navigation graph.

inline operator Unit

Adds a destination to this NavGraph.

inline operator Unit

Add all destinations from another collection to this one.

Inherited functions

From kotlin.collections.Iterable
From androidx.navigation.NavDestination
Unit
addArgument(argumentName: String, argument: NavArgument)

Sets an argument type for an argument name

Unit
addDeepLink(navDeepLink: NavDeepLink)

Add a deep link to this destination.

Unit
addDeepLink(uriPattern: String)

Add a deep link to this destination.

String?
fillInLabel(context: Context, bundle: SavedState?)

Parses a dynamic label containing arguments into a String.

NavAction?

Returns the NavAction for the given action ID.

open Boolean
hasDeepLink(deepLinkRequest: NavDeepLinkRequest)

Checks the given NavDeepLinkRequest, and determines whether it matches a NavDeepLink added to the destination by a call to addDeepLink.

open Boolean
hasDeepLink(deepLink: Uri)

Checks the given deep link Uri, and determines whether it matches a Uri pattern added to the destination by a call to addDeepLink .

Unit
putAction(actionId: @IdRes Int, action: NavAction)

Sets the NavAction destination for an action ID.

Unit
putAction(actionId: @IdRes Int, destId: @IdRes Int)

Creates a NavAction for the given destId and associates it with the actionId.

Unit
removeAction(actionId: @IdRes Int)

Unsets the NavAction for an action ID.

Unit
removeArgument(argumentName: String)

Unsets the argument type for an argument name.

Inherited properties

From androidx.navigation.NavDestination
Map<StringNavArgument>

The arguments supported by this destination.

Int

The destination's unique ID.

CharSequence?

The descriptive label of this destination.

String

The name associated with this destination's Navigator.

NavGraph?

Gets the NavGraph that contains this destination.

String?

The destination's unique route.

Public companion functions

findStartDestination

fun NavGraph.findStartDestination(): NavDestination

Finds the actual start destination of the graph, handling cases where the graph's starting destination is itself a NavGraph.

Returns
NavDestination

the actual startDestination of the given graph.

Public constructors

Added in 1.0.0
NavGraph(navGraphNavigator: Navigator<NavGraph>)
Parameters
navGraphNavigator: Navigator<NavGraph>

The NavGraphNavigator which this destination will be associated with. Generally retrieved via a NavController'sNavigatorProvider.getNavigator method.

Public functions

addAll

Added in 1.0.0
fun addAll(other: NavGraph): Unit

Add all destinations from another collection to this one. As each destination has at most one parent, the destinations will be removed from the given NavGraph.

Parameters
other: NavGraph

collection of destinations to add. All destinations will be removed from this graph after being added to this graph.

addDestination

Added in 1.0.0
fun addDestination(node: NavDestination): Unit

Adds a destination to this NavGraph. The destination must have an NavDestination.id id} set.

The destination must not have a parent set. If the destination is already part of a navigation graph, call remove before calling this method.

Parameters
node: NavDestination

destination to add

Throws
kotlin.IllegalArgumentException

if destination does not have an id, the destination has the same id as the graph, or the destination already has a parent.

addDestinations

Added in 1.0.0
fun addDestinations(nodes: Collection<NavDestination?>): Unit

Adds multiple destinations to this NavGraph. Each destination must have an NavDestination.id id} set.

Each destination must not have a parent set. If any destination is already part of a navigation graph, call remove before calling this method.

Parameters
nodes: Collection<NavDestination?>

destinations to add

addDestinations

Added in 1.0.0
fun addDestinations(vararg nodes: NavDestination): Unit

Adds multiple destinations to this NavGraph. Each destination must have an NavDestination.id id} set.

Each destination must not have a parent set. If any destination is already part of a navigation graph, call remove before calling this method.

Parameters
vararg nodes: NavDestination

destinations to add

clear

Added in 1.0.0
fun clear(): Unit

Clear all destinations from this navigation graph.

equals

open operator fun equals(other: Any?): Boolean

findNode

inline fun <T : Any?> findNode(): NavDestination?

Finds a destination in the collection by route from KClass. This will recursively check the parent of this navigation graph if node is not found in this navigation graph.

Parameters
<T : Any?>

Route from a KClass to locate

Returns
NavDestination?

the node with route - the node must have been created with a route from KClass

findNode

Added in 1.0.0
fun findNode(resId: @IdRes Int): NavDestination?

Finds a destination in the collection by ID. This will recursively check the parent of this navigation graph if node is not found in this navigation graph.

Parameters
resId: @IdRes Int

ID to locate

Returns
NavDestination?

the node with ID resId

findNode

Added in 2.9.0-alpha04
fun findNode(route: KClass<*>): NavDestination?

Finds a destination in the collection by route from KClass. This will recursively check the parent of this navigation graph if node is not found in this navigation graph.

Parameters
route: KClass<*>

Route from a KClass to locate

Returns
NavDestination?

the node with route - the node must have been created with a route from KClass

findNode

Added in 2.8.0
fun <T : Any?> findNode(route: T?): NavDestination?

Finds a destination in the collection by route from Object. This will recursively check the parent of this navigation graph if node is not found in this navigation graph.

Parameters
route: T?

Route to locate

Returns
NavDestination?

the node with route - the node must have been created with a route from KClass

findNode

Added in 2.4.0
fun findNode(route: String?): NavDestination?

Finds a destination in the collection by route. This will recursively check the parent of this navigation graph if node is not found in this navigation graph.

Parameters
route: String?

Route to locate

Returns
NavDestination?

the node with route

getStartDestination

Added in 1.0.0
Deprecated in 2.4.0
fun getStartDestination(): @IdRes Int

Gets the starting destination for this NavGraph. When navigating to the NavGraph, this destination is the one the user will initially see.

Returns
@IdRes Int

the start destination

hashCode

open fun hashCode(): Int

iterator

Added in 1.0.0
final operator fun iterator(): MutableIterator<NavDestination>
Throws
kotlin.NoSuchElementException

if there no more elements

onInflate

open fun onInflate(context: Context, attrs: AttributeSet): Unit

Called when inflating a destination from a resource.

Parameters
context: Context

local context performing inflation

attrs: AttributeSet

attrs to parse during inflation

remove

Added in 1.0.0
fun remove(node: NavDestination): Unit

Remove a given destination from this NavGraph

Parameters
node: NavDestination

the destination to remove.

setStartDestination

inline fun <T : Any> setStartDestination(): Unit

Sets the starting destination for this NavGraph.

This will override any previously set startDestinationId

Parameters
<T : Any>

The route of the destination as a KClass to be shown when navigating to this NavGraph.

setStartDestination

Added in 1.0.0
fun setStartDestination(startDestId: Int): Unit

Sets the starting destination for this NavGraph.

This will clear any previously set startDestinationRoute.

Parameters
startDestId: Int

The id of the destination to be shown when navigating to this NavGraph.

setStartDestination

Added in 2.9.0-alpha04
fun <T : Any> setStartDestination(startDestRoute: KClass<T>): Unit

Sets the starting destination for this NavGraph.

This will override any previously set startDestinationId

Parameters
startDestRoute: KClass<T>

The KClass of route T to be shown when navigating to this NavGraph.

setStartDestination

Added in 2.8.0
fun <T : Any> setStartDestination(startDestRoute: T): Unit

Sets the starting destination for this NavGraph.

This will override any previously set startDestinationId

Parameters
startDestRoute: T

The route of the destination as an object to be shown when navigating to this NavGraph.

setStartDestination

Added in 2.4.0
fun setStartDestination(startDestRoute: String): Unit

Sets the starting destination for this NavGraph.

This will override any previously set startDestinationId

Parameters
startDestRoute: String

The route of the destination to be shown when navigating to this NavGraph.

toString

open fun toString(): String

Public properties

startDestinationId

Added in 2.4.0
val startDestinationIdInt

The starting destination id for this NavGraph. When navigating to the NavGraph, the destination represented by this id is the one the user will initially see.

startDestinationRoute

Added in 2.4.0
val startDestinationRouteString?

The route for the starting destination for this NavGraph. When navigating to the NavGraph, the destination represented by this route is the one the user will initially see.

Extension functions

operator fun NavGraph.contains(id: @IdRes Int): Boolean

Returns true if a destination with id is found in this navigation graph.

inline operator fun <T : Any> NavGraph.contains(route: KClass<T>): Boolean

Returns true if a destination with route is found in this navigation graph.

operator fun NavGraph.contains(route: String): Boolean

Returns true if a destination with route is found in this navigation graph.

operator fun <T : Any> NavGraph.contains(route: T): Boolean

Returns true if a destination with route is found in this navigation graph.

inline operator fun NavGraph.get(id: @IdRes Int): NavDestination

Returns the destination with id.

Throws
kotlin.IllegalArgumentException

if no destination is found with that id.

inline operator fun <T : Any> NavGraph.get(route: KClass<T>): NavDestination

Returns the destination with route from KClass.

Throws
kotlin.IllegalArgumentException

if no destination is found with that route.

inline operator fun NavGraph.get(route: String): NavDestination

Returns the destination with route.

Throws
kotlin.IllegalArgumentException

if no destination is found with that route.

inline operator fun <T : Any> NavGraph.get(route: T): NavDestination

Returns the destination with route from an Object.

Throws
kotlin.IllegalArgumentException

if no destination is found with that route.

inline operator fun NavGraph.minusAssign(node: NavDestination): Unit

Removes node from this navigation graph.

inline operator fun NavGraph.plusAssign(node: NavDestination): Unit

Adds a destination to this NavGraph. The destination must have an id set.

The destination must not have a parent set. If the destination is already part of a NavGraph, call NavGraph.remove before calling this method.

Parameters
node: NavDestination

destination to add

inline operator fun NavGraph.plusAssign(other: NavGraph): Unit

Add all destinations from another collection to this one. As each destination has at most one parent, the destinations will be removed from the given NavGraph.

Parameters
other: NavGraph

collection of destinations to add. All destinations will be removed from the parameter graph after being added to this graph.