NavGraph

public class NavGraph
extends NavDestination implements Iterable<NavDestination>

java.lang.Object
   ↳ 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

NavGraph(NavigatorProvider navigatorProvider)

Construct a new NavGraph.

NavGraph(Navigator<? extends NavGraph> navGraphNavigator)

Construct a new NavGraph.

Public methods

void addAll(NavGraph other)

Add all destinations from another collection to this one.

void addDestination(NavDestination node)

Adds a destination to this NavGraph.

void addDestinations(Collection<NavDestination> nodes)

Adds multiple destinations to this NavGraph.

void addDestinations(NavDestination... nodes)

Adds multiple destinations to this NavGraph.

void clear()

Clear all destinations from this navigation graph.

NavDestination findNode(int resid)

Finds a destination in the collection by ID.

int getStartDestination()

Returns the starting destination for this NavGraph.

Iterator<NavDestination> iterator()
void onInflate(Context context, AttributeSet attrs)

Called when inflating a destination from a resource.

void remove(NavDestination node)

Remove a given destination from this NavGraph

void setStartDestination(int startDestId)

Sets the starting destination for this NavGraph.

Inherited methods

Public constructors

NavGraph

NavGraph (NavigatorProvider navigatorProvider)

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

Parameters
navigatorProvider NavigatorProvider: The NavController which this NavGraph will be associated with.

NavGraph

NavGraph (Navigator<? extends NavGraph> navGraphNavigator)

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

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

Public methods

addAll

void addAll (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
other NavGraph: collection of destinations to add. All destinations will be removed from this graph after being added to this graph.

addDestination

void addDestination (NavDestination node)

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

void addDestinations (Collection<NavDestination> nodes)

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 Collection: destinations to add

addDestinations

void addDestinations (NavDestination... nodes)

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

void clear ()

Clear all destinations from this navigation graph.

findNode

NavDestination findNode (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
resid int: ID to locate

Returns
NavDestination the node with ID resid

getStartDestination

int getStartDestination ()

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

Returns
int

iterator

Iterator<NavDestination> iterator ()

Returns
Iterator<NavDestination>

onInflate

void onInflate (Context context, 
                AttributeSet attrs)

Called when inflating a destination from a resource.

Parameters
context Context: local context performing inflation

attrs AttributeSet: attrs to parse during inflation

remove

void remove (NavDestination node)

Remove a given destination from this NavGraph

Parameters
node NavDestination: the destination to remove.

setStartDestination

void setStartDestination (int startDestId)

Sets the starting destination for this NavGraph.

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