0
0
Android Kotlinmobile~5 mins

Deep links in Android Kotlin

Choose your learning style9 modes available
Introduction

Deep links let your app open specific screens directly from outside, like from a website or another app. This helps users jump straight to what they want.

You want users to open a product page in your app from a web link.
You want to send a notification that opens a specific chat in your messaging app.
You want to let other apps open your app to a certain screen.
You want to improve user experience by skipping the home screen and going straight to content.
Syntax
Android Kotlin
<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="https"
          android:host="www.example.com"
          android:pathPrefix="/path" />
</intent-filter>

This code goes inside your AndroidManifest.xml under an <activity> tag.

The scheme, host, and pathPrefix define which links open your app.

Examples
This filter opens your app when a user clicks links like https://www.shop.com/product/123.
Android Kotlin
<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="https"
          android:host="www.shop.com"
          android:pathPrefix="/product" />
</intent-filter>
This filter handles custom links like myapp://open to open your app.
Android Kotlin
<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="open" />
</intent-filter>
Sample App

This activity shows the link used to open the app if any. If opened normally, it shows a default message.

Android Kotlin
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val data = intent?.data
        val textView = findViewById<TextView>(R.id.textView)

        if (data != null) {
            textView.text = "Opened with link: ${data.toString()}"
        } else {
            textView.text = "Opened normally"
        }
    }
}
OutputSuccess
Important Notes

Remember to test your deep links by clicking links on your device or using adb commands.

Use adb shell am start -W -a android.intent.action.VIEW -d "https://www.example.com/path/item123" your.package.name to test.

Make sure your app handles the link data safely to avoid crashes.

Summary

Deep links let apps open specific screens from outside links.

Define deep links in AndroidManifest.xml with intent filters.

Handle the incoming link data in your activity to show the right content.