Create custom DeepLinkMatcher classes

If the provided matchers aren't sufficient for your use cases, you can create your own by extending the DeepLinkMatcher class.

When extending DeepLinkMatcher<T, R>, you must specify two type arguments: the destination navigation key type (T : Any) and the match result type (R : DeepLinkMatcher.MatchResult<T>). If your matcher doesn't need custom result ranking or extra metadata, use DeepLinkMatcher.MatchResult<T> as the second type argument.

For example, here's a basic implementation of a TelDeepLinkMatcher that supports tel URIs (such as tel:5550100). Because tel URIs are opaque, UriDeepLinkMatcher doesn't support them:

Rank custom MatchResult classes

If there's a meaningful way to compare match results from your custom matcher, you should extend DeepLinkMatcher.MatchResult and override the compareTo method to rank results.

When you create a custom MatchResult subclass, update your matcher's class declaration to specify that subclass as its second type parameter R (such as class TelDeepLinkMatcher : DeepLinkMatcher<DialerKey, TelMatchResult>()). This lets callers access your custom result properties without unchecked downcasting.

For example, to support wildcard pattern matching in TelDeepLinkMatcher, you can implement a TelMatchResult to ensure exact matches rank higher than wildcard matches. If the matcher is wrapped in withBackStack, unwrap WrappedMatchResult before comparing so that precedence is evaluated against the underlying match result: