Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

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 default arguments that will be applied when navigating to that destination. These arguments can be overridden at the time of navigation.

Summary

Public constructors

NavDestination(Navigator<? extends NavDestination> navigator)

NavDestinations should be created via Navigator.createDestination().

Public methods

void addDeepLink(String uriPattern)

Add a deep link to this destination.

void addDefaultArguments(Bundle args)

Merges a bundle of arguments into the current default arguments for this destination.

NavAction getAction(int id)

Returns the destination ID for a given action.

Bundle getDefaultArguments()

Returns the destination's default arguments bundle.

int getId()

Returns the destination's unique ID.

CharSequence getLabel()

Gets the descriptive label of this destination.

Navigator getNavigator()

Returns the destination's Navigator.

NavGraph getParent()

Gets the NavGraph that contains this destination.

void navigate(Bundle args, NavOptions navOptions)

Navigates to this destination.

void onInflate(Context context, AttributeSet attrs)

Called when inflating a destination from a resource.

void putAction(int actionId, NavAction action)

Sets a destination ID for an action ID.

void putAction(int actionId, int destId)

Sets a destination ID for an action ID.

void removeAction(int actionId)

Unsets the destination ID for an action ID.

void setDefaultArguments(Bundle args)

Sets the destination's default arguments bundle.

void setId(int id)

Sets the destination's unique ID.

void setLabel(CharSequence label)

Sets the descriptive label of this destination.

Inherited methods

Public constructors

public NavDestination (Navigator<? extends NavDestination> navigator)

NavDestinations should be created via Navigator.createDestination().

Parameters
navigator Navigator

Public methods

addDeepLink

public void addDeepLink (String uriPattern)

Add a deep link to this destination. Matching Uris sent to NavController.onHandleDeepLink(Intent) 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

addDefaultArguments

public void addDefaultArguments (Bundle args)

Merges a bundle of arguments into the current default arguments for this destination. New values with the same keys will replace old values with those keys.

Parameters
args Bundle: arguments to add

getAction

public 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

getDefaultArguments

public Bundle getDefaultArguments ()

Returns the destination's default arguments bundle.

Returns
Bundle the default arguments bundle

getId

public 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 CharSequence getLabel ()

Gets the descriptive label of this destination.

Returns
CharSequence

getNavigator

public Navigator getNavigator ()

Returns the destination's Navigator.

Returns
Navigator this destination's navigator

getParent

public 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

public void navigate (Bundle args, 
                NavOptions navOptions)

Navigates to this destination.

Uses the configured navigator to navigate to this destination. Apps should not call this directly, instead use NavController's navigation methods to ensure consistent back stack tracking and behavior.

Parameters
args Bundle: arguments to the new destination

navOptions NavOptions: options for navigation

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 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 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 void removeAction (int actionId)

Unsets the destination ID for an action ID.

Parameters
actionId int: action ID to remove

setDefaultArguments

public void setDefaultArguments (Bundle args)

Sets the destination's default arguments bundle.

Parameters
args Bundle: the new bundle to set

setId

public 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 void setLabel (CharSequence label)

Sets the descriptive label of this destination.

Parameters
label CharSequence: A descriptive label of this destination.