It's happening now, watch the livestream.

NavDestination

public class NavDestination
extends Object

java.lang.Object
   ↳ androidx.navigation.NavDestination


NavDestination represents one node within an overall navigation graph.

Each destination is associated with a Navigator which knows how to navigate to this particular destination.

Destinations declare a set of actions that they support. These actions form a navigation API for the destination; the same actions declared on different destinations that fill similar roles allow application code to navigate based on semantic intent.

Each destination has a set of arguments that will be applied when navigating to that destination. Any default values for those arguments can be overridden at the time of navigation.

Summary

Nested classes

@interface NavDestination.ClassType

This optional annotation allows tooling to offer auto-complete for the android:name attribute. 

Public constructors

NavDestination(Navigator<? extends NavDestination> navigator)

NavDestinations should be created via Navigator.createDestination().

NavDestination(String navigatorName)

NavDestinations should be created via Navigator.createDestination().

Public methods

final void addArgument(String argumentName, NavArgument argument)

Sets an argument type for an argument name

final void addDeepLink(String uriPattern)

Add a deep link to this destination.

final NavAction getAction(int id)

Returns the destination ID for a given action.

final Map<String, NavArgument> getArguments()

Get the arguments supported by this destination.

final int getId()

Returns the destination's unique ID.

final CharSequence getLabel()

Gets the descriptive label of this destination.

final String getNavigatorName()

Returns the name associated with this destination's Navigator.

final NavGraph getParent()

Gets the NavGraph that contains this destination.

boolean hasDeepLink(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(String) .

void onInflate(Context context, AttributeSet attrs)

Called when inflating a destination from a resource.

final void putAction(int actionId, NavAction action)

Sets a destination ID for an action ID.

final void putAction(int actionId, int destId)

Sets a destination ID for an action ID.

final void removeAction(int actionId)

Unsets the destination ID for an action ID.

final void removeArgument(String argumentName)

Unsets the argument type for an argument name.

final void setId(int id)

Sets the destination's unique ID.

final void setLabel(CharSequence label)

Sets the descriptive label of this destination.

String toString()

Protected methods

static <C> Class<? extends C> parseClassFromName(Context context, String name, Class<? extends C> expectedClassType)

Parse the class associated with this destination from a raw name, generally extracted from the android:name attribute added to the destination's XML.

Inherited methods

Public constructors

public NavDestination (Navigator<? extends NavDestination> navigator)

NavDestinations should be created via Navigator.createDestination().

This constructor requires that the given Navigator has a Navigator.Name annotation.

Parameters
navigator Navigator

public NavDestination (String navigatorName)

NavDestinations should be created via Navigator.createDestination().

Parameters
navigatorName String

Public methods

addArgument

public final void addArgument (String argumentName, 
                NavArgument argument)

Sets an argument type for an argument name

Parameters
argumentName String: argument object to associate with destination

argument NavArgument

addDeepLink

public final void addDeepLink (String uriPattern)

Add a deep link to this destination. Matching Uris sent to NavController.handleDeepLink(Intent) or NavController.navigate(Uri) will trigger navigating to this destination.

In addition to a direct Uri match, the following features are supported:

  • Uris without a scheme are assumed as http and https. For example, www.example.com will match http://www.example.com and https://www.example.com.
  • Placeholders in the form of {placeholder_name} matches 1 or more characters. The String value of the placeholder will be available in the arguments Bundle with a key of the same name. For example, http://www.example.com/users/{id} will match http://www.example.com/users/4.
  • The .* wildcard can be used to match 0 or more characters.
These Uris can be declared in your navigation XML files by adding one or more <deepLink app:uri="uriPattern" /> elements as a child to your destination.

Deep links added in navigation XML files will automatically replace instances of ${applicationId} with the applicationId of your app. Programmatically added deep links should use Context.getPackageName() directly when constructing the uriPattern.

Parameters
uriPattern String: The uri pattern to add as a deep link

getAction

public final NavAction getAction (int id)

Returns the destination ID for a given action. This will recursively check the parent of this destination if the action destination is not found in this destination.

Parameters
id int: action ID to fetch

Returns
NavAction destination ID mapped to the given action id, or 0 if none

getArguments

public final Map<String, NavArgument> getArguments ()

Get the arguments supported by this destination. Returns a read-only map of argument names to NavArgument objects that can be used to check the type, default value and nullability of the argument.

To add and remove arguments for this NavDestination use addArgument(String, NavArgument) and removeArgument(String).

Returns
Map<String, NavArgument> Read-only map of argument names to arguments.

getId

public final int getId ()

Returns the destination's unique ID. This should be an ID resource generated by the Android resource system.

Returns
int this destination's ID

getLabel

public final CharSequence getLabel ()

Gets the descriptive label of this destination.

Returns
CharSequence

getNavigatorName

public final String getNavigatorName ()

Returns the name associated with this destination's Navigator.

Returns
String the name associated with this destination's navigator

getParent

public final NavGraph getParent ()

Gets the NavGraph that contains this destination. This will be set when a destination is added to a NavGraph via NavGraph.addDestination(NavDestination).

Returns
NavGraph

hasDeepLink

public boolean hasDeepLink (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(String) . It returns true if the deep link is a valid match, and false otherwise.

This should be called prior to NavController.navigate(Uri) to ensure the deep link can be navigated to.

Parameters
deepLink Uri: to the destination reachable from the current NavGraph

Returns
boolean True if the deepLink exists for the destination.

onInflate

public 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

putAction

public final void putAction (int actionId, 
                NavAction action)

Sets a destination ID for an action ID.

Parameters
actionId int: action ID to bind

action NavAction: action to associate with this action ID

putAction

public final void putAction (int actionId, 
                int destId)

Sets a destination ID for an action ID.

Parameters
actionId int: action ID to bind

destId int: destination ID for the given action

removeAction

public final void removeAction (int actionId)

Unsets the destination ID for an action ID.

Parameters
actionId int: action ID to remove

removeArgument

public final void removeArgument (String argumentName)

Unsets the argument type for an argument name.

Parameters
argumentName String: argument to remove

setId

public final void setId (int id)

Sets the destination's unique ID. This should be an ID resource generated by the Android resource system.

Parameters
id int: this destination's new ID

setLabel

public final void setLabel (CharSequence label)

Sets the descriptive label of this destination.

Parameters
label CharSequence: A descriptive label of this destination.

toString

public String toString ()

Returns
String

Protected methods

parseClassFromName

protected static Class<? extends C> parseClassFromName (Context context, 
                String name, 
                Class<? extends C> expectedClassType)

Parse the class associated with this destination from a raw name, generally extracted from the android:name attribute added to the destination's XML. This should be the class providing the visual representation of the destination that the user sees after navigating to this destination.

This method does name -> Class caching and should be strongly preferred over doing your own parsing if your Navigator supports the android:name attribute to give consistent behavior across all Navigators.

Parameters
context Context: Context providing the package name for use with relative class names and the ClassLoader

name String: Absolute or relative class name. Null names will be ignored.

expectedClassType Class: The expected class type

Returns
Class<? extends C> The parsed class

Throws
IllegalArgumentException if the class is not found in the provided Context's ClassLoader or if the class is not of the expected type