tezvyn:

Deep Linking with Android's Navigation Component

AI-drafted, machine-checkedSource: developer.android.comadvanced
Deep Linking with Android's Navigation Component

Deep linking with the Navigation Component treats app screens like URL-addressable pages. It's for handling push notifications or web links that go to specific content.

WHY IT EXISTS Apps need to handle entry from multiple points, not just the main launcher icon. A user might tap a push notification, a link in an email, or a Google search result. Manually parsing incoming URIs and managing the navigation and back stack for these cases is complex and error-prone. The Navigation Component automates this routing.

THE MENTAL MODEL Think of your app's navigation graph as a sitemap and deep links as the unique URLs for each page. Instead of writing boilerplate code to handle intents, you simply declare a URI for a destination in your navigation graph. The Navigation Component then acts as a router, matching incoming links to the correct screen and handling argument parsing for you.

HOW IT WORKS In your navigation graph XML file, you add a <deepLink> element to a destination. You specify its uri attribute with a URL pattern, like "myapp://products/{productId}". The curly braces denote a placeholder for an argument. When an incoming intent contains a matching URI, the Navigation Component automatically navigates to that destination. It parses the value from the placeholder (e.g., "123" for productId) and passes it as an argument to the destination fragment or activity. You must also declare a corresponding <intent-filter> in your AndroidManifest.xml to let the Android system know your app can handle these URIs.

WHEN TO USE IT Use deep links for any entry point into your app from the outside. This is the standard mechanism for handling push notifications that lead to specific content, links shared on social media, making app content appear in Google Search results, or responding to custom URI schemes from other apps.

WHEN NOT TO USE IT Do not use deep links for navigating between screens within your app. For internal navigation, always use Navigation Component actions (e.g., navController.navigate(R.id.action_...)). Using deep links internally is an anti-pattern that complicates the back stack and bypasses the type-safe, structured nature of navigation actions.

ONE CANONICAL EXAMPLE A user taps a push notification for a sale on a specific item. The notification contains the URI "https://www.example.com/items/456". Your navigation graph has a destination for the item detail screen with a <deepLink> for that URI pattern. The Navigation Component intercepts the intent, navigates to the item detail screen, and passes "456" as the item ID argument. Crucially, it can also construct a synthetic back stack, so pressing 'back' takes the user to the item list screen, not out of the app.

Read the original → developer.android.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.