NavDestination


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

NavDestinations should be created via Navigator.createDestination.

Summary

Nested types

@Retention(value = AnnotationRetention.BINARY)
@Target(allowedTargets = [AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASS])
annotation NavDestination.ClassType

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

Public companion functions

inline Boolean

Checks if the NavDestination's route was generated from T

Boolean
<T : Any> NavDestination.hasRoute(route: KClass<T>)

Checks if the NavDestination's route was generated from T

Protected companion functions

Class<C?>
<C : Any?> parseClassFromName(
    context: Context,
    name: String,
    expectedClassType: Class<C?>
)

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

Public companion properties

Sequence<NavDestination>

Provides a sequence of the NavDestination's hierarchy.

Public constructors

NavDestination(navigatorName: String)

NavDestinations should be created via Navigator.createDestination.

Public functions

Unit
addArgument(argumentName: String, argument: NavArgument)

Sets an argument type for an argument name

Unit
addDeepLink(navDeepLink: NavDeepLink)

Add a deep link to this destination.

Unit
addDeepLink(uriPattern: String)

Add a deep link to this destination.

open operator Boolean
equals(other: Any?)
String?
fillInLabel(context: Context, bundle: Bundle?)

Parses a dynamic label containing arguments into a String.

NavAction?

Returns the NavAction for the given action ID.

open Boolean
hasDeepLink(deepLinkRequest: NavDeepLinkRequest)

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

open Boolean
hasDeepLink(deepLink: Uri)

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

open Int
open Unit

Called when inflating a destination from a resource.

Unit
putAction(actionId: @IdRes Int, action: NavAction)

Sets the NavAction destination for an action ID.

Unit
putAction(actionId: @IdRes Int, destId: @IdRes Int)

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

Unit
removeAction(actionId: @IdRes Int)

Unsets the NavAction for an action ID.

Unit
removeArgument(argumentName: String)

Unsets the argument type for an argument name.

open String

Public properties

Map<StringNavArgument>

The arguments supported by this destination.

Int

The destination's unique ID.

CharSequence?

The descriptive label of this destination.

String

The name associated with this destination's Navigator.

NavGraph?

Gets the NavGraph that contains this destination.

String?

The destination's unique route.

Public companion functions

inline fun <T : Any> NavDestination.hasRoute(): Boolean

Checks if the NavDestination's route was generated from T

Returns true if equal, false otherwise.

Parameters
<T : Any>

the route from KClass

fun <T : Any> NavDestination.hasRoute(route: KClass<T>): Boolean

Checks if the NavDestination's route was generated from T

Returns true if equal, false otherwise.

Parameters
route: KClass<T>

the route from KClass

Protected companion functions

parseClassFromName

Added in 2.4.0
protected fun <C : Any?> parseClassFromName(
    context: Context,
    name: String,
    expectedClassType: Class<C?>
): Class<C?>

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<C?>

The expected class type

Returns
Class<C?>

The parsed class

Throws
kotlin.IllegalArgumentException

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

Public companion properties

val NavDestination.hierarchySequence<NavDestination>

Provides a sequence of the NavDestination's hierarchy. The hierarchy starts with this destination itself and is then followed by this destination's NavDestination.parent, then that graph's parent, and up the hierarchy until you've reached the root navigation graph.

Public constructors

Added in 1.0.0
NavDestination(navigatorName: String)
Added in 1.0.0
NavDestination(navigator: Navigator<NavDestination>)

NavDestinations should be created via Navigator.createDestination.

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

Public functions

addArgument

Added in 1.0.0
fun addArgument(argumentName: String, argument: NavArgument): Unit

Sets an argument type for an argument name

Parameters
argumentName: String

argument object to associate with destination

argument: NavArgument

argument object to associate with destination

addDeepLink

Added in 2.3.0
fun addDeepLink(navDeepLink: NavDeepLink): Unit

Add a deep link to this destination. Uris that match the given NavDeepLink uri sent to NavController.handleDeepLink or NavController.navigate 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.

Custom actions and mimetypes are also supported by NavDeepLink and can be declared in your navigation XML files by adding <app:action="android.intent.action.SOME_ACTION" /> or <app:mimetype="type/subtype" /> as part of your deepLink declaration.

Deep link Uris, actions, and mimetypes 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.

When matching deep links for calls to NavController.handleDeepLink or NavController.navigate the order of precedence is as follows: the deep link with the most matching arguments will be chosen, followed by the deep link with a matching action, followed by the best matching mimeType (e.i. when matching mimeType image/jpg: image/ * *\/jpg *\/ *).

Parameters
navDeepLink: NavDeepLink

The NavDeepLink to add as a deep link

addDeepLink

Added in 1.0.0
fun addDeepLink(uriPattern: String): Unit

Add a deep link to this destination. Matching Uris sent to NavController.handleDeepLink or NavController.navigate 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 parsed 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

equals

open operator fun equals(other: Any?): Boolean

fillInLabel

Added in 2.6.0
fun fillInLabel(context: Context, bundle: Bundle?): String?

Parses a dynamic label containing arguments into a String.

Supports String Resource arguments by parsing R.string values of ReferenceType arguments found in android:label into their String values.

Returns null if label is null.

Returns the original label if the label was a static string.

Parameters
context: Context

Context used to resolve a resource's name

bundle: Bundle?

Bundle containing the arguments used in the label

Returns
String?

The parsed string or null if the label is null

Throws
kotlin.IllegalArgumentException

if an argument provided in the label cannot be found in the bundle, or if the label contains a string template but the bundle is null

getAction

Added in 1.0.0
fun getAction(id: @IdRes Int): NavAction?

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

Parameters
id: @IdRes Int

action ID to fetch

Returns
NavAction?

the NavAction mapped to the given action id, or null if one has not been set

hasDeepLink

Added in 2.3.0
open fun hasDeepLink(deepLinkRequest: NavDeepLinkRequest): Boolean

Checks the given NavDeepLinkRequest, and determines whether it matches a NavDeepLink added to the destination by a call to addDeepLink. It returns true if the request is a valid match, and false otherwise.

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

Parameters
deepLinkRequest: NavDeepLinkRequest

to the destination reachable from the current NavGraph

Returns
Boolean

True if the deepLink exists for the destination.

hasDeepLink

Added in 2.1.0
open fun hasDeepLink(deepLink: Uri): Boolean

Checks the given deep link Uri, and determines whether it matches a Uri pattern added to the destination by a call to addDeepLink . It returns true if the deep link is a valid match, and false otherwise.

This should be called prior to NavController.navigate 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.

hashCode

open fun hashCode(): Int

onInflate

Added in 1.0.0
@CallSuper
open fun onInflate(context: Context, attrs: AttributeSet): Unit

Called when inflating a destination from a resource.

Parameters
context: Context

local context performing inflation

attrs: AttributeSet

attrs to parse during inflation

putAction

Added in 1.0.0
fun putAction(actionId: @IdRes Int, action: NavAction): Unit

Sets the NavAction destination for an action ID.

Parameters
actionId: @IdRes Int

action ID to bind

action: NavAction

action to associate with this action ID

Throws
kotlin.UnsupportedOperationException

this destination is considered a terminal destination and does not support actions

putAction

Added in 1.0.0
fun putAction(actionId: @IdRes Int, destId: @IdRes Int): Unit

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

Parameters
actionId: @IdRes Int

action ID to bind

destId: @IdRes Int

destination ID for the given action

removeAction

Added in 1.0.0
fun removeAction(actionId: @IdRes Int): Unit

Unsets the NavAction for an action ID.

Parameters
actionId: @IdRes Int

action ID to remove

removeArgument

Added in 1.0.0
fun removeArgument(argumentName: String): Unit

Unsets the argument type for an argument name.

Parameters
argumentName: String

argument to remove

toString

open fun toString(): String

Public properties

arguments

Added in 1.0.0
val argumentsMap<StringNavArgument>

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

Returns
Map<StringNavArgument>

Read-only map of argument names to arguments.

id

Added in 1.0.0
var idInt

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

If using safe args, setting this manually will override the ID that was set based on route from KClass.

label

Added in 1.0.0
var labelCharSequence?

The descriptive label of this destination.

Added in 1.0.0
val navigatorNameString

The name associated with this destination's Navigator.

parent

Added in 1.0.0
val parentNavGraph?

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

route

Added in 2.4.0
var routeString?

The destination's unique route. Setting this will also update the id of the destinations so custom destination ids should only be set after setting the route.

Returns
String?

this destination's route, or null if no route is set

Throws
kotlin.IllegalArgumentException

is the given route is empty