Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Deep linking in Android allows an app to be started up and navigated to a specific activity using a URL. This is useful for linking users directly from a website, an email, or another app to a particular location within your app.
There are three types of deep linking methods available in Android:
Here's a simple example of how to implement traditional deep linking:
Modify your AndroidManifest.xml
to include an intent filter inside the <activity>
tag that you want to be deep-linked.
<activity android:name=".DeepLinkedActivity"> <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:host="example.com" android:scheme="https" android:pathPrefix="/path/to/content" /> </intent-filter> </activity>
This configuration means the app will listen for links that are structured like: https://example.com/path/to/content
.
In the targeted Activity
, you need to handle the incoming intent and extract data:
class DeepLinkedActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_deep_linked) intent?.let { handleDeepLink(it) } } private fun handleDeepLink(intent: Intent) { val action = intent.action val data = intent.dataString if (Intent.ACTION_VIEW == action && data != null) { // Handle the deep link. You might extract parameters from the URL and use them in your app. } } }
To test deep linking, you can use adb
(Android Debug Bridge) by running the following command in your terminal:
adb shell am start -W -a android.intent.action.VIEW -d "https://example.com/path/to/content" your.package.name
Replace your.package.name
with your app's package name.
Deep links can potentially be a security concern. Always validate data passed in from deep links.
Android App Links are a more advanced version that requires website verification, ensuring only the app associated with the website can handle its deep links without user prompts.
Firebase Dynamic Links are useful when you want the link to work even if the user needs to install the app first. The link will survive the app installation process.
Setting up deep linking in Android manifest:
<activity android:name=".DeepLinkActivity" android:label="Deep Link Activity"> <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="http" android:host="example.com" android:pathPrefix="/deep-link" /> </intent-filter> </activity>
Handling deep links in Android activities:
class DeepLinkActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_deep_link) // Extract data from the deep link val uri = intent.data val path = uri?.path val host = uri?.host // Process the deep link data } }
Customizing deep link behavior in Android:
class DeepLinkActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_deep_link) // Extract and customize deep link behavior val uri = intent.data val path = uri?.path val host = uri?.host // Customize behavior based on the deep link data } }
Passing parameters through deep links in Android:
<data android:scheme="http" android:host="example.com" android:pathPrefix="/deep-link/{param}" />Code (Kotlin - DeepLinkActivity):
class DeepLinkActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_deep_link) // Extract parameters from the deep link val uri = intent.data val paramValue = uri?.lastPathSegment // Process the parameter value } }
Using URI schemes for deep linking in Android:
<data android:scheme="myscheme" android:host="myhost" android:path="/deep-link" />Code (Kotlin - DeepLinkActivity):
class DeepLinkActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_deep_link) // Extract data from the deep link with custom URI scheme val uri = intent.data val path = uri?.path val host = uri?.host // Process the deep link data } }
Deferred deep linking in Android example:
class MyApplication : Application() { override fun onCreate() { super.onCreate() AppLinkData.fetchDeferredAppLinkData(this) { appLinkData -> if (appLinkData != null) { // Handle deferred deep link data val uri = appLinkData.targetUri // Process the deferred deep link data } } } }