0
0
Android Kotlinmobile~20 mins

Deep links in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: DeepLinkDemoScreen
This screen handles deep links to show a message passed in the URL.
Target UI
-------------------------
| Deep Link Demo        |
|-----------------------|
| Message:              |
| [                    ]|
|                       |
| (Open app via deep link)|
-------------------------
Handle deep links with scheme 'myapp' and host 'showmessage'
Extract 'msg' query parameter from the deep link URL
Display the extracted message on the screen
Show a default message if no deep link or no message parameter
Starter Code
Android Kotlin
package com.example.deeplinkdemo

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class DeepLinkDemoScreen : AppCompatActivity() {
    private lateinit var messageTextView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_deep_link_demo_screen)

        messageTextView = findViewById(R.id.messageTextView)

        // TODO: Handle the incoming intent for deep link
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
package com.example.deeplinkdemo

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class DeepLinkDemoScreen : AppCompatActivity() {
    private lateinit var messageTextView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_deep_link_demo_screen)

        messageTextView = findViewById(R.id.messageTextView)

        val data: Uri? = intent?.data
        if (data != null && data.scheme == "myapp" && data.host == "showmessage") {
            val message = data.getQueryParameter("msg") ?: "No message found"
            messageTextView.text = message
        } else {
            messageTextView.text = "Welcome! Open this screen via a deep link."
        }
    }
}

We check if the intent has data (a Uri). Then we verify the scheme is 'myapp' and the host is 'showmessage' to confirm it's our deep link. We extract the 'msg' query parameter from the Uri and display it in the TextView. If no deep link or message is found, we show a default welcome message.

Final Result
Completed Screen
-------------------------
| Deep Link Demo        |
|-----------------------|
| Message:              |
| Hello from deep link! |
|                       |
| (Open app via deep link)|
-------------------------
When user opens the app via a deep link like myapp://showmessage?msg=Hello%20from%20deep%20link!, the message text updates to 'Hello from deep link!'
If app is opened normally, the message shows the default welcome text
Stretch Goal
Add support for deep links with a path parameter instead of query parameter, e.g., myapp://showmessage/HelloWorld
💡 Hint
Use Uri.getPathSegments() to extract the message from the path segments