What is Deep Link?
- A Deep Link is a URI of any scheme that takes the user directly to a specific part of your application.
- Using this URL we can send a message to our application with parameters.
What is it used for?
- Deep Links are used for directing users to specific parts of your application, or other applications all together.
- This allows for the user to bypass certain parts of your application allowing for an easier experience.
How to create Deep Links
- You will have to first define an
intent-filterfor it within yourAndroidManifest.xmlfile.- These drive users to the right activity in your application.
Looks like this:
<activity Â
android:name=".MyMapActivity"Â Â
android:exported="true"Â Â
...>Â Â
<intent-filter android:autoVerfiy="true">Â Â Â Â
<action android:name="android.intent.action.VIEW" />Â Â Â Â
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Scheme and host for the deep link -->
<data android:scheme="example" android:host="content" android:pathPrefix="/path/to/content" />Â
</intent-filter>
</activity>Let’s go through what each tag and it’s attribute means.
- The
<activity>tag is defines the activities we have within our application.android:nameattribute specifies the name of the activity.android:exportedattribute declares that this activity is accessible outside this application.
- The
<intent-filter>tag declares what type of intents this application can respond to.android:autoVerifyattribute ensures that the deep link is verified within our system.- If it’s set to
true, the system tries to verify that the specified domain is associated with your application.
- If it’s set to
<action>tag specified the action that your application can handle.android:nameattribute defines the action nameandroid:intent.action.VIEWtells the system that this activity can respond to aviewactions.
<category>tag adds a category to ourintent-filter, which specifies additional information about the kinds ofIntentsthe activity can handle.android.intent.category.DEFAULTdeclares our activity as an entry point to our applicationandroid.intent.category.BROWSABLEdeclares that our activity is capable of being reached via a web link. This makes it accessible to web browsers and other applications.
<data>tag specifies the data that theURIpattern can handle.android:schemeattribute defines the URI scheme(e.g.HTTP,HTTPS, or a custom scheme likeexample).android:hostattribute specifies the host part of the URI(e.g.,www.example.comor a custom host likecontent).android:pathPrefixattribute defines the initial part of the path that theURImust match.- In this case, it matches with URIs starting with
/path/to/content - So with our given configuration our
MainActivitycan handle deep links that match the following patternexample://content/path/to/contentexamplebeing the custom schemecontentbeing the custom host/path/to/contentbeing the path
- In this case, it matches with URIs starting with
Deep Linking to launch other applications
- Configure Deep Linking in Your Application
- Handle the Deep Link in Your Activity
- Launch Your App via Deep Link from Another Application
1. Configure Deep Linking in Your Application
- Set up deep link in your applications
AndroidManifest.xmlExample:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- Main entry point of the app -->
<activity android:name=".MainActivity">
<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" />
<!-- Scheme and host for the deep link -->
<data android:scheme="example" android:host="content" android:pathPrefix="/path/to/content" />
</intent-filter>
</activity>
</application>
</manifest>
In this configuration:
- The
android:schemeis set toexample. - The
android:hostis set tocontent. - The
android:pathPrefixis set to/path/to/content.
2. Handle the Deep Link in Your Activity]
In your MainActivity, handle the incoming deep link.
Example:
package com.example.yourapp
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
handleIntent(intent)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleIntent(intent)
}
private fun handleIntent(intent: Intent) {
val action = intent.action
val data: Uri? = intent.data
if (Intent.ACTION_VIEW == action && data != null) {
val path = data.path
// Parse the URI and navigate to the appropriate content in your app
when (path) {
"/path/to/content" -> {
// Navigate to the specific content
Log.i("MainActivity", "Deep link received with path: $path")
}
else -> {
// Handle other paths
Log.i("MainActivity", "Unknown path: $path")
}
}
}
}
}3. Launch Your App via Deep Link from Another Application
In the other application, create an Intent that uses a deep link URI to launch your application.
Example code from other application:
package com.example.otherapp
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Create an intent with the deep link URI
val deepLinkUri = Uri.parse("example://content/path/to/content")
val intent = Intent(Intent.ACTION_VIEW, deepLinkUri)
// Check if there is an app that can handle this intent
val packageManager = packageManager
val resolveInfo = packageManager.resolveActivity(intent, 0)
if (resolveInfo != null) {
startActivity(intent)
} else {
// Handle the case where no app can handle the intent
// Optionally, redirect to the Play Store or show an error message
}
}
}Summary
-
Configure Deep Linking in Your App:
- Set up the
intent-filterin your app’sAndroidManifest.xmlto handle the specific deep link URI.
- Set up the
-
Handle the Deep Link in Your Activity:
- Implement logic in your activity to parse and respond to the deep link.
-
Launch Your App via Deep Link from Another App:
- Create an intent with the deep link URI and use it to launch your app from another application.