Android Navigation Deep Link — Complete Guide
In this tutorial, you'll learn about Android Navigation Deep Link. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your deep link opens the app but shows a blank screen, or the URI parameters don't get parsed, or the back stack is wrong after deep linking.
Wrong Approach ❌
<!-- Deep link without proper URI pattern -->
<deepLink app:uri="myapp://details" /> <!-- No parameters -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
</intent-filter>
Output: Deep link opens but with hardcoded destination — no parameter matching.
Right Approach ✅
<!-- Correct deep link with parameters -->
<deepLink app:uri="myapp://details/{itemId}" />
<deepLink app:uri="https://www.example.com/items/{itemId}" />
<!-- In Manifest -->
<activity android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="myapp"
android:host="details" />
</intent-filter>
<nav-graph android:value="@navigation/nav_graph" />
</activity>
// Handling deep link programmatically
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// NavController handles pending deep links automatically
// For manual handling:
intent?.data?.let { uri ->
findNavController(R.id.nav_host).handleDeepLink(intent)
}
}
}
Output: Deep link navigates to correct destination with parsed parameters.
Prevention
- Use
{paramName}placeholders in URI patterns. - Register
<nav-graph>in the manifest to auto-handle deep links. - Use
pendingIntentfor notification deep links withNavDeepLinkBuilder. - Test deep links with
adb shell am start -d "myapp://details/123".
Common Mistakes with navigation deep link
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro