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 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 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.

boolean
equals(Object other)
final NavDestination
<T extends Object> findNode()

Finds a destination in the collection by route from KClass.

final NavDestination
findNode(@IdRes int resId)

Finds a destination in the collection by ID.

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.

final @IdRes int

This method is deprecated. Use getStartDestinationId instead.

final @IdRes int

The starting destination id for this NavGraph.

final String

The route for the starting destination for this NavGraph.

int
final @NonNull Iterator<@NonNull NavDestination>
void

Called when inflating a destination from a resource.

final void

Remove a given destination from this NavGraph

final void
<T extends Object> setStartDestination()

Sets the starting destination for this NavGraph.

final void
setStartDestination(int startDestId)

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.

@NonNull String

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 String
fillInLabel(@NonNull Context context, Bundle bundle)

Parses a dynamic label containing arguments into a String.

final NavAction
getAction(@IdRes int id)

Returns the NavAction for the given action ID.

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

The arguments supported by this destination.

final @IdRes int

The destination's unique ID.

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 NavDeepLinkRequest, and determines whether it matches a NavDeepLink added to the destination by a call to addDeepLink.

boolean
hasDeepLink(@NonNull Uri deepLink)

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

final void
putAction(@IdRes int actionId, @NonNull NavAction action)

Sets the NavAction destination for an action ID.

final void
putAction(@IdRes int actionId, @IdRes int destId)

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

final void
removeAction(@IdRes int actionId)

Unsets the NavAction for an action ID.

final void
removeArgument(@NonNull String argumentName)

Unsets the argument type for an argument name.

final void
setId(int id)

The destination's unique ID.

final void

The descriptive label of 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.

equals

public boolean equals(Object other)

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 1.0.0
public final NavDestination findNode(@IdRes int resId)

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
@IdRes int resId

ID to locate

Returns
NavDestination

the node with ID resId

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.

getStartDestination

Added in 1.0.0
Deprecated in 2.4.0
public final @IdRes int getStartDestination()

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

getStartDestinationId

Added in 2.4.0
public final @IdRes int getStartDestinationId()

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.

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.

hashCode

public int hashCode()

iterator

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

if there no more elements

onInflate

public void onInflate(@NonNull Context context, @NonNull AttributeSet attrs)

Called when inflating a destination from a resource.

Parameters
@NonNull Context context

local context performing inflation

@NonNull AttributeSet attrs

attrs to parse during inflation

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 1.0.0
public final void setStartDestination(int startDestId)

Sets the starting destination for this NavGraph.

This will clear any previously set startDestinationRoute.

Parameters
int startDestId

The id of the destination 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.

toString

public @NonNull String toString()

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 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.