Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

NavGraph

open class NavGraph : NavDestination, MutableIterable<NavDestination!>
kotlin.Any
   ↳ androidx.navigation.NavDestination
   ↳ androidx.navigation.NavGraph

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.

Summary

Public constructors

<init>(@NonNull navGraphNavigator: Navigator<out NavGraph!>)

Construct a new NavGraph.

Public methods

Unit
addAll(@NonNull 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
addDestinations(@NonNull vararg nodes: NavDestination!)

Adds multiple destinations to this NavGraph.

Unit

Clear all destinations from this navigation graph.

NavDestination?
findNode(@IdRes resid: Int)

Finds a destination in the collection by ID.

Int

Returns the starting destination for this NavGraph.

MutableIterator<NavDestination!>

open Unit
onInflate(@NonNull context: Context, @NonNull attrs: AttributeSet)

Unit
remove(@NonNull node: NavDestination)

Remove a given destination from this NavGraph

Unit
setStartDestination(@IdRes startDestId: Int)

Sets the starting destination for this NavGraph.

open String

Inherited functions

Extension functions

From androidx.navigation
operator Boolean
NavGraph.contains(@IdRes id: Int)

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

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

Returns the destination with id.

operator Unit

Removes node from this navigation graph.

operator Unit

Adds a destination to this NavGraph.

operator Unit

Add all destinations from another collection to this one.

Public constructors

<init>

NavGraph(@NonNull navGraphNavigator: Navigator<out NavGraph!>)

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

Parameters
navGraphNavigator Navigator<out NavGraph!>: The NavGraphNavigator which this destination will be associated with. Generally retrieved via a NavController's NavigatorProvider#getNavigator(Class) method.

Public methods

addAll

fun addAll(@NonNull 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

fun addDestination(@NonNull node: NavDestination): Unit

Adds a destination to this NavGraph. The destination must have an NavDestination#getId() id} set.

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

Parameters
node NavDestination: destination to add

addDestinations

fun addDestinations(@NonNull nodes: MutableCollection<NavDestination!>): Unit

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

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

Parameters
nodes MutableCollection<NavDestination!>: destinations to add

addDestinations

fun addDestinations(@NonNull vararg nodes: NavDestination!): Unit

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

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

Parameters
nodes NavDestination!: destinations to add

clear

fun clear(): Unit

Clear all destinations from this navigation graph.

findNode

@Nullable fun findNode(@IdRes resid: 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 Int: ID to locate
Return
NavDestination?: the node with ID resid

getStartDestination

@IdRes fun getStartDestination(): Int

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

Return
Int:

iterator

@NonNull fun iterator(): MutableIterator<NavDestination!>

onInflate

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

remove

fun remove(@NonNull node: NavDestination): Unit

Remove a given destination from this NavGraph

Parameters
node NavDestination: the destination to remove.

setStartDestination

fun setStartDestination(@IdRes startDestId: Int): Unit

Sets the starting destination for this NavGraph.

Parameters
startDestId Int: The id of the destination to be shown when navigating to this NavGraph.

toString

@NonNull open fun toString(): String