NavDestination
public class NavDestination
ActivityNavigator.Destination |
NavDestination for activity navigation |
DialogFragmentNavigator.Destination |
NavDestination specific to |
DynamicIncludeGraphNavigator.DynamicIncludeNavGraph |
The graph for dynamic-include. |
FragmentNavigator.Destination |
NavDestination specific to |
NavGraph |
NavGraph is a collection of |
DynamicFragmentNavigator.Destination |
Destination for dynamic feature navigator. |
DynamicGraphNavigator.DynamicNavGraph |
The |
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 putAction 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)This optional annotation allows tooling to offer auto-complete for the |
public static class NavDestination.Companion |
Public constructors |
|---|
NavDestination(@NonNull String navigatorName) |
NavDestination(@NonNull Navigator<@NonNull NavDestination> navigator)NavDestinations should be created via |
Public methods |
|
|---|---|
final void |
addArgument(@NonNull String argumentName, @NonNull NavArgument argument)Sets an argument type for an argument name |
final void |
addDeepLink(@NonNull NavDeepLink navDeepLink)Add a deep link to this destination. |
final void |
addDeepLink(@NonNull String uriPattern)Add a deep link to this destination. |
boolean |
|
final String |
fillInLabel(@NonNull Context context, Bundle bundle)Parses a dynamic label containing arguments into a String. |
final NavAction |
Returns the |
final @NonNull Map<@NonNull String, @NonNull NavArgument> |
The arguments supported by this destination. |
@NonNull String |
|
static final @NonNull Sequence<@NonNull NavDestination> |
getHierarchy(@NonNull NavDestination receiver)Provides a sequence of the NavDestination's hierarchy. |
final @IdRes int |
getId()The destination's unique ID. |
final CharSequence |
getLabel()The descriptive label of this destination. |
final @NonNull String |
The name associated with this destination's |
final NavGraph |
Gets the |
final String |
getRoute()The destination's unique route. |
boolean |
hasDeepLink(@NonNull NavUri deepLink)Checks the given deep link |
boolean |
hasDeepLink(@NonNull NavDeepLinkRequest deepLinkRequest)Checks the given |
boolean |
hasDeepLink(@NonNull Uri deepLink) |
static final boolean |
<T extends Object> hasRoute(@NonNull NavDestination receiver) |
static final boolean |
Checks if the NavDestination's route was generated from |
int |
hashCode() |
void |
Called when inflating a destination from a resource. |
final void |
Sets the |
final void |
Creates a |
final void |
removeAction(@IdRes int actionId)Unsets the |
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 |
setLabel(CharSequence label)The descriptive label of this destination. |
final void |
Gets the |
final void |
The destination's unique route. |
@NonNull String |
toString() |
Protected methods |
|
|---|---|
static final @NonNull Class<C> |
<C extends Object> parseClassFromName(Parse the class associated with this destination from a raw name, generally extracted from the |
Public constructors
NavDestination
public NavDestination(@NonNull Navigator<@NonNull NavDestination> navigator)
NavDestinations should be created via Navigator.createDestination.
This constructor requires that the given Navigator has a Navigator.Name annotation.
Public methods
addArgument
public final void addArgument(@NonNull String argumentName, @NonNull NavArgument argument)
Sets an argument type for an argument name
| Parameters | |
|---|---|
@NonNull String argumentName |
argument object to associate with destination |
@NonNull NavArgument argument |
argument object to associate with destination |
addDeepLink
public final void addDeepLink(@NonNull NavDeepLink navDeepLink)
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 SavedState 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 | |
|---|---|
@NonNull NavDeepLink navDeepLink |
The NavDeepLink to add as a deep link |
| See also | |
|---|---|
handleDeepLink |
|
navigate |
addDeepLink
public final void addDeepLink(@NonNull String uriPattern)
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.comwill matchhttp://www.example.comandhttps://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 argumentsSavedStatewith a key of the same name. For example,http://www.example.com/users/{id}will matchhttp://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.
| See also | |
|---|---|
handleDeepLink |
|
navigate |
|
addDeepLink |
fillInLabel
public final String fillInLabel(@NonNull Context context, Bundle bundle)
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 | |
|---|---|
@NonNull Context context |
Context used to resolve a resource's name |
Bundle bundle |
SavedState 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
public final NavAction getAction(@IdRes int id)
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 | |
|---|---|
@IdRes int id |
action ID to fetch |
getArguments
public final @NonNull Map<@NonNull String, @NonNull NavArgument> getArguments()
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.
getHierarchy
public static final @NonNull Sequence<@NonNull NavDestination> getHierarchy(@NonNull NavDestination receiver)
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.
getId
public final @IdRes int getId()
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.
getLabel
public final CharSequence getLabel()
The descriptive label of this destination.
getNavigatorName
public final @NonNull String getNavigatorName()
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.
getRoute
public final String getRoute()
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 |
hasDeepLink
public boolean hasDeepLink(@NonNull NavUri deepLink)
Checks the given deep link NavUri, 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.
| Returns | |
|---|---|
boolean |
True if the deepLink exists for the destination. |
| See also | |
|---|---|
addDeepLink |
|
navigate |
|
hasDeepLink |
hasDeepLink
public boolean hasDeepLink(@NonNull NavDeepLinkRequest deepLinkRequest)
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 | |
|---|---|
@NonNull NavDeepLinkRequest deepLinkRequest |
to the destination reachable from the current NavGraph |
| Returns | |
|---|---|
boolean |
True if the deepLink exists for the destination. |
| See also | |
|---|---|
addDeepLink |
|
navigate |
hasRoute
public static final boolean <T extends Object> hasRoute(@NonNull NavDestination receiver)
Checks if the NavDestination's route was generated from T
Returns true if equal, false otherwise.
| Parameters | |
|---|---|
<T extends Object> |
the route from KClass |
hasRoute
public static final boolean <T extends Object> hasRoute(
@NonNull NavDestination receiver,
@NonNull KClass<@NonNull T> route
)
Checks if the NavDestination's route was generated from T
Returns true if equal, false otherwise.
onInflate
@CallSuper
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 |
putAction
public final void putAction(@IdRes int actionId, @NonNull NavAction action)
Sets the NavAction destination for an action ID.
| Parameters | |
|---|---|
@IdRes int actionId |
action ID to bind |
@NonNull NavAction action |
action to associate with this action ID |
| Throws | |
|---|---|
kotlin.UnsupportedOperationException |
this destination is considered a terminal destination and does not support actions |
putAction
public final void putAction(@IdRes int actionId, @IdRes int destId)
Creates a NavAction for the given destId and associates it with the actionId.
removeAction
public final void removeAction(@IdRes int actionId)
Unsets the NavAction for an action ID.
| Parameters | |
|---|---|
@IdRes int actionId |
action ID to remove |
removeArgument
public final void removeArgument(@NonNull String argumentName)
Unsets the argument type for an argument name.
setId
public final void setId(int id)
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.
setLabel
public final void setLabel(CharSequence label)
The descriptive label of this destination.
setParent
public final void setParent(NavGraph parent)
Gets the NavGraph that contains this destination. This will be set when a destination is added to a NavGraph via NavGraph.addDestination.
setRoute
public final void setRoute(String route)
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 | |
|---|---|
void |
this destination's route, or null if no route is set |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
is the given route is empty |
Protected methods
parseClassFromName
protected static final @NonNull Class<C> <C extends Object> parseClassFromName(
@NonNull Context context,
@NonNull String name,
@NonNull Class<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 | |
|---|---|
@NonNull Context context |
Context providing the package name for use with relative class names and the ClassLoader |
@NonNull String name |
Absolute or relative class name. Null names will be ignored. |
@NonNull Class<C> expectedClassType |
The expected class type |
| 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 |