Deep Links, Navigation Component and Advanced Task Behaviour
Deep Links, Navigation Component and Advanced Task Behaviour
1. What is a deep link?
A deep link opens a specific destination inside an application.
Example:
https://example.com/products/101
|
v
ProductDetails screenSources can include:
* Website
* SMS
* Notification
* QR code
* Another application
* Search result
Android deep links use the Intent system to route users directly to app content.
---
2. Types of deep links
Custom URI
myshop://products/101Easy to configure, but another application could register the same scheme.
Web deep link
https://example.com/products/101The system may ask the user which application should open it.
Android App Link
https://example.com/products/101The website and Android application are verified using a trusted association.
After verification, supported links can open directly in the application without a chooser dialog.
---
3. Basic manifest deep link
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter android:autoVerify="true">
<action
android:name="android.intent.action.VIEW" />
<category
android:name="android.intent.category.DEFAULT" />
<category
android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="example.com"
android:pathPrefix="/products" />
</intent-filter>
</activity>Read the URI:
val uri = intent?.data
val productId =
uri?.lastPathSegmentAlso handle deep links delivered to an existing Activity:
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
handleDeepLink(intent.data)
}---
4. Navigation Component
The Navigation Component manages navigation between app destinations.
Main elements:
NavHost
Displays the current destination
NavGraph
Defines destinations and connections
NavController
Performs navigation and manages back stack
NavDestination
Represents one screenThe NavController coordinates destination changes, back-stack operations and deep-link handling.
---
5. Navigation graph example
<navigation
android:id="@+id/main_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.HomeFragment">
<action
android:id="@+id/action_home_to_details"
app:destination="@id/detailsFragment" />
</fragment>
<fragment
android:id="@+id/detailsFragment"
android:name="com.example.DetailsFragment">
<argument
android:name="productId"
app:argType="string" />
</fragment>
</navigation>Navigate:
findNavController().navigate(
HomeFragmentDirections
.actionHomeToDetails("P100")
)---
6. Navigation Component back stack
Home
|
Details
|
PaymentNavController stack:
Top
Payment
Details
Home
BottomNavigate:
navController.navigate(
R.id.detailsFragment
)Go back:
navController.popBackStack()Pop up to a destination:
navController.popBackStack(
R.id.homeFragment,
false
)The NavController maintains destinations as a last-in, first-out stack.
---
7. Deep link using Navigation Component
<fragment
android:id="@+id/productFragment"
android:name="com.example.ProductFragment">
<argument
android:name="productId"
app:argType="string" />
<deepLink
app:uri=
"https://example.com/products/{productId}" />
</fragment>URL:
https://example.com/products/101Argument:
productId = 101Navigation deep links can match URI placeholders and convert them into destination arguments.
---
8. Back versus Up navigation
Back
Back follows the reverse history of screens the user visited.
App A
|
App B opened from App A
|
Back
|
Return to App ABack may exit the current application.
Up
Up moves through the logical hierarchy inside the current application.
Product Details
|
Up
|
Product List
|
Up
|
HomeUp should not unexpectedly exit the application.
Within the app’s own task, Up and Back often behave identically. They differ most clearly when the user enters through a deep link from another application.
Deep-link example
Email application
|
Deep link
|
Product DetailsPress Back:
Return to email applicationPress Up:
Product List
|
HomeThe application may construct a logical or synthetic back stack for Up navigation.
---
9. Task affinity
Task affinity tells Android which task an Activity prefers to belong to.
By default, Activities in the same application normally share the same affinity.
<activity
android:name=".WorkActivity"
android:taskAffinity="com.example.work" />Task affinity matters mainly with:
* FLAG_ACTIVITY_NEW_TASK
* singleTask
* Activity reparenting
* Multiple task workflows
* Document-centric applications
Most normal applications should not change the default task affinity.
Incorrect affinity configuration can produce confusing Recents and Back behavior.
---
10. Document-centric behaviour
Document-centric behavior allows different documents or items to appear as separate tasks in Recents.
Example:
Recent Apps
Document A
Document B
Document CInstead of:
Recent Apps
One application taskUse:
val intent = Intent(
this,
DocumentActivity::class.java
).apply {
data = Uri.parse(
"content://documents/document-101"
)
addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
}
startActivity(intent)FLAG_ACTIVITY_NEW_DOCUMENT asks Android to treat the document as a separate task-like entry in Recents.
---
11. documentLaunchMode
Manifest options include:
<activity
android:name=".DocumentActivity"
android:documentLaunchMode="intoExisting" />Common values:
| Value | Behaviour |
|---|---|
none | Normal Activity behaviour |
intoExisting | Reuse matching document task |
always | Always create a separate document task |
never | Do not launch as a document task |
Use cases:
* Document editor
* Browser tabs
* Email drafts
* File viewer
* Multiple independent work items
Avoid document mode for ordinary screen navigation.
---
12. Complete navigation flow
External website
|
Deep link Intent
|
Android resolves link
|
App task found or created
|
MainActivity receives Intent
|
NavController handles route
|
Target destination displayed
|
Synthetic back stack created if needed
|
Back returns through history
Up returns through app hierarchy---
13. Common navigation mistakes
* Opening a deep link but ignoring authentication state
* Losing the requested destination after login
* Not validating URI parameters
* Not handling deep links in onNewIntent()
* Creating duplicate destinations
* Confusing Fragment back stack with Activity task stack
* Using Up exactly like Back in external deep-link cases
* Changing task affinity unnecessarily
* Using document mode for normal screens
* Passing complete objects instead of IDs between destinations
---
14. Login-protected deep-link flow
Deep link received
|
Parse and validate URL
|
Is user logged in?
|
+---+---+
| |
Yes No
| |
| Open Login
| |
| Login success
| |
+---+---+
|
Open requested destinationStore only the validated destination information:
data class PendingDestination(
val productId: String
)After login, navigate to the requested screen.
---
Final interview answer
> Android navigation exists at two levels. The operating system manages tasks and Activity back stacks, while FragmentManager or NavController manages destinations inside an Activity. Deep links use Intents to open specific application content. Back follows the user’s actual navigation history, while Up follows the logical hierarchy inside the application. Launch modes, Intent flags, task affinity and document modes control advanced task behaviour, but they should be used carefully because incorrect configuration can create unexpected Back and Recents behaviour.