Support deep links

Starting in version 1.2.0, Navigation 3 supports deep linking to destinations in your app using the DeepLinkRequest and DeepLinkMatcher classes.

To support deep links in your app, complete the following steps:

  1. Define intent filters in your AndroidManifest.xml to specify which URIs your app can handle.
  2. Create DeepLinkMatcher instances to map incoming requests to your navigation keys.
  3. Match incoming requests in your activity's onCreate or onNewIntent method and update your back stack accordingly.

Create a DeepLinkRequest

A DeepLinkRequest represents an incoming deep link. It contains a DeepLinkUri and optional RequestExtras with additional information, such as the intent action or MIME type.

Provide DeepLinkRequest extras

To store additional information related to the deep link, use the RequestExtras class. To make defining and instantiating extras type-safe, the library provides the RequestExtrasKey interface and requestExtras DSL.

Additionally, the library provides two extras keys and associated helpers:

To define your own custom extras, implement the RequestExtrasKey interface with a typed generic:

You can also use emptyRequestExtras() to construct an empty instance, or combine extras using the + (plus) and - (minus) operators.

Create a DeepLinkRequest from an Intent

On Android, you can create a DeepLinkRequest directly from an Intent. When constructed this way, the DeepLinkRequest is built as follows:

A DeepLinkMatcher maps incoming DeepLinkRequest instances to navigation keys that can be added to your app's back stack. Navigation 3 provides three built-in matchers: UriDeepLinkMatcher for pattern-based URI matching, StaticKeyDeepLinkMatcher for basic links, and BackStackMatcher for building synthetic back stacks. The library also supports custom matchers for use cases not covered by the built-in matchers.

For more information, see Create DeepLinkMatchers.

Add intent filters

To enable a deep link to start your activity, you must define the matching <intent-filter> elements in your app's AndroidManifest.xml. For more information, see Add intent filters for incoming links.

Match an incoming request

After you've created your DeepLinkMatcher instances, you can match incoming requests in your activity.

To match an incoming request, complete the following steps:

  1. Instantiate your DeepLinkMatcher instances.
  2. Collate all of your DeepLinkMatcher instances, either explicitly or by using multibindings.
  3. Create a DeepLinkRequest from the incoming Intent.
  4. Match the request against all matchers and find the best match.
  5. Create the back stack from the match result.