public class NavGraph extends NavDestination implements 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 getStartDestination 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 constructors

NavGraph(@NonNull Navigator<@NonNull NavGraph> navGraphNavigator)

Public methods

final void

Add all destinations from another collection to this one.

final void

Adds a destination to this NavGraph.

final void

Adds multiple destinations to this NavGraph.

final void

Adds multiple destinations to this NavGraph.

final void

Clear all destinations from this navigation graph.

final NavDestination
<T extends Object> findNode()

Finds a destination in the collection by route from KClass.

final NavDestination

Finds a destination in the collection by route from KClass.

final NavDestination
<T extends Object> findNode(T route)

Finds a destination in the collection by route from Object.

final NavDestination

Finds a destination in the collection by route.

static final @NonNull NavDestination

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

@NonNull String
final @NonNull String
final String

The route for the starting destination for this NavGraph.

@NonNull Iterator<@NonNull NavDestination>
final void

Remove a given destination from this NavGraph

final void
<T extends Object> setStartDestination()

Sets the starting destination for this NavGraph.

final void
<T extends Object> setStartDestination(@NonNull T startDestRoute)

Sets the starting destination for this NavGraph.

final void

Sets the starting destination for this NavGraph.

final void
setStartDestinationRoute(String startDestinationRoute)

Extension functions

final boolean
NavGraphKt.contains(@NonNull NavGraph receiver, @IdRes int id)

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

final boolean
<T extends Object> NavGraphKt.contains(
    @NonNull NavGraph receiver,
    @NonNull KClass<@NonNull T> route
)

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

final boolean

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

final boolean
<T extends Object> NavGraphKt.contains(@NonNull NavGraph receiver, @NonNull T route)

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

final @NonNull NavDestination
NavGraphKt.get(@NonNull NavGraph receiver, @IdRes int id)

Returns the destination with id.

final @NonNull NavDestination
<T extends Object> NavGraphKt.get(
    @NonNull NavGraph receiver,
    @NonNull KClass<@NonNull T> route
)

Returns the destination with route from KClass.

final @NonNull NavDestination

Returns the destination with route.

final @NonNull NavDestination
<T extends Object> NavGraphKt.get(@NonNull NavGraph receiver, @NonNull T route)

Returns the destination with route from an Object.

final void
NavGraphKt.minusAssign(
    @NonNull NavGraph receiver,
    @NonNull NavDestination node
)

Removes node from this navigation graph.

final void
NavGraphKt.plusAssign(
    @NonNull NavGraph receiver,
    @NonNull NavDestination node
)

Adds a destination to this NavGraph.

final void

Add all destinations from another collection to this one.

Inherited methods

From kotlin.collections.Iterable
From androidx.navigation.NavDestination
final void
addArgument(@NonNull String argumentName, @NonNull NavArgument argument)

Sets an argument type for an argument name

final void

Add a deep link to this destination.

final void
addDeepLink(@NonNull String uriPattern)

Add a deep link to this destination.

final @NonNull Map<@NonNull String, @NonNull NavArgument>

The arguments supported by this destination.

final CharSequence

The descriptive label of this destination.

final @NonNull String

The name associated with this destination's Navigator.

final NavGraph

Gets the NavGraph that contains this destination.

final String

The destination's unique route.

boolean

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

boolean

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

final void
removeArgument(@NonNull String argumentName)

Unsets the argument type for an argument name.

final void

The descriptive label of this destination.

final void

Gets the NavGraph that contains this destination.

final void

The destination's unique route.

Public constructors

Added in 1.0.0
public NavGraph(@NonNull Navigator<@NonNull NavGraph> navGraphNavigator)
Parameters
@NonNull Navigator<@NonNull NavGraph> navGraphNavigator

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

Public methods

addAll

Added in 1.0.0
public final void addAll(@NonNull NavGraph other)

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
@NonNull NavGraph other

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
public final void addDestination(@NonNull NavDestination node)

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
@NonNull NavDestination node

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
public final void addDestinations(@NonNull Collection<NavDestination> nodes)

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
@NonNull Collection<NavDestination> nodes

destinations to add

addDestinations

Added in 1.0.0
public final void addDestinations(@NonNull NavDestination nodes)

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
@NonNull NavDestination nodes

destinations to add

clear

Added in 1.0.0
public final void clear()

Clear all destinations from this navigation graph.

findNode

public final NavDestination <T extends Object> findNode()

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 extends Object>

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.9.0-alpha07
public final NavDestination findNode(@NonNull KClass<@NonNull ?> route)

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
@NonNull KClass<@NonNull ?> route

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
public final NavDestination <T extends Object> findNode(T route)

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
T route

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
public final NavDestination findNode(String route)

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
String route

Route to locate

Returns
NavDestination

the node with route

findStartDestination

Added in 2.4.0
public static final @NonNull NavDestination findStartDestination(@NonNull NavGraph receiver)

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

Returns
@NonNull NavDestination

the actual startDestination of the given graph.

getDisplayName

public @NonNull String getDisplayName()

getStartDestDisplayName

public final @NonNull String getStartDestDisplayName()

getStartDestinationRoute

Added in 2.4.0
public final String getStartDestinationRoute()

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.

iterator

Added in 1.0.0
public @NonNull Iterator<@NonNull NavDestinationiterator()
Throws
kotlin.NoSuchElementException

if there no more elements

remove

Added in 1.0.0
public final void remove(@NonNull NavDestination node)

Remove a given destination from this NavGraph

Parameters
@NonNull NavDestination node

the destination to remove.

setStartDestination

public final void <T extends Object> setStartDestination()

Sets the starting destination for this NavGraph.

This will override any previously set startDestinationId

Parameters
<T extends Object>

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

setStartDestination

Added in 2.8.0
public final void <T extends Object> setStartDestination(@NonNull T startDestRoute)

Sets the starting destination for this NavGraph.

This will override any previously set startDestinationId

Parameters
@NonNull T startDestRoute

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

setStartDestination

Added in 2.4.0
public final void setStartDestination(@NonNull String startDestRoute)

Sets the starting destination for this NavGraph.

This will override any previously set startDestinationId

Parameters
@NonNull String startDestRoute

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

setStartDestinationRoute

public final void setStartDestinationRoute(String startDestinationRoute)

Extension functions

NavGraphKt.contains

public final boolean NavGraphKt.contains(@NonNull NavGraph receiver, @IdRes int id)

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

NavGraphKt.contains

public final boolean <T extends Object> NavGraphKt.contains(
    @NonNull NavGraph receiver,
    @NonNull KClass<@NonNull T> route
)

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

NavGraphKt.contains

public final boolean NavGraphKt.contains(@NonNull NavGraph receiver, @NonNull String route)

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

NavGraphKt.contains

public final boolean <T extends Object> NavGraphKt.contains(@NonNull NavGraph receiver, @NonNull T route)

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

public final @NonNull NavDestination NavGraphKt.get(@NonNull NavGraph receiver, @IdRes int id)

Returns the destination with id.

Throws
kotlin.IllegalArgumentException

if no destination is found with that id.

public final @NonNull NavDestination <T extends Object> NavGraphKt.get(
    @NonNull NavGraph receiver,
    @NonNull KClass<@NonNull T> route
)

Returns the destination with route from KClass.

Throws
kotlin.IllegalArgumentException

if no destination is found with that route.

public final @NonNull NavDestination NavGraphKt.get(@NonNull NavGraph receiver, @NonNull String route)

Returns the destination with route.

Throws
kotlin.IllegalArgumentException

if no destination is found with that route.

public final @NonNull NavDestination <T extends Object> NavGraphKt.get(@NonNull NavGraph receiver, @NonNull T route)

Returns the destination with route from an Object.

Throws
kotlin.IllegalArgumentException

if no destination is found with that route.

NavGraphKt.minusAssign

public final void NavGraphKt.minusAssign(
    @NonNull NavGraph receiver,
    @NonNull NavDestination node
)

Removes node from this navigation graph.

NavGraphKt.plusAssign

public final void NavGraphKt.plusAssign(
    @NonNull NavGraph receiver,
    @NonNull NavDestination node
)

Adds a destination to this NavGraph. The destination must have an NavDestination.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
@NonNull NavDestination node

destination to add

NavGraphKt.plusAssign

public final void NavGraphKt.plusAssign(@NonNull NavGraph receiver, @NonNull NavGraph other)

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
@NonNull NavGraph other

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