ডিপ লিঙ্ক স্ট্যাটিক URI রেসিপি
এই রেসিপি দেখায় যে কীভাবে স্ট্যাটিক Uri-এর সাথে ডিপ লিঙ্ক করতে হয়।
রেসিপির কম্পোনেন্ট
রেসিপিতে দুটি অ্যাক্টিভিটি আছে:
- ডিপ লিঙ্ক URI সহ একটি Intent তৈরি ও শুরু করতে
StaticUriDeepLinkActivity MainActivityহল ডিপ লিঙ্কের টার্গেট অ্যাক্টিভিটি, এটি এমন একটি অ্যাপকে দেখায় যেখানে ব্যবহারকারীরা ডিপ লিঙ্ক করতে পারেন।
দেখানো ডিপ লিঙ্ক কীভাবে কাজ করে
- ডিপ লিঙ্ক সোর্স (
StaticUriDeepLinkActivity) URI নির্ধারণ করে এবং ডিপ লিঙ্কের সাথে যুক্ত করার জন্য একটি ইনটেন্ট তৈরি করে। - অ্যাপ (
MainActivity) একটি নেভিগেশন কী (HomeKey) ঘোষণা করে।HomeKeyযে ডিপ লিঙ্কিং কাজ করে তা বোঝাতে, অ্যাপটিHomeKey-এর সাথেUriDeepLinkMatcherঘোষণা করে, এর সাথেHomeKey-এর কাজ করা uri প্যাটার্নও থাকে। MainActivityonCreate ইনটেন্ট সহ একটিDeepLinkRequestইনস্ট্যানশিয়েট করে এবংMatchResultপেতেUriDeepLinkMatcher-এর সাথে সেটি ম্যাচ করে।MatchResultনন-নাল হলে, অ্যাপটি ফলাফলের মাধ্যমে পাওয়া কীতে নেভিগেট করে। অন্যথায়, ডিপ লিঙ্ক কাজ করে না এবং অ্যাপটিFallbackস্ক্রিনে নেভিগেট করে।
package com.example.nav3recipes.deeplink.handlerequests.staticuri import android.content.Intent import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.core.net.toUri import androidx.lifecycle.compose.dropUnlessResumed import com.example.nav3recipes.common.deeplink.EntryScreen import com.example.nav3recipes.common.deeplink.PaddedButton import com.example.nav3recipes.common.deeplink.TextContent import com.example.nav3recipes.deeplink.handlerequests.uriarguments.ui.PATH_BASE import com.example.nav3recipes.ui.setEdgeToEdgeConfig const val HOME_URI = "$PATH_BASE/home" class StaticUriDeepLinkActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { setEdgeToEdgeConfig() super.onCreate(savedInstanceState) setContent { EntryScreen("Deep link url:") { TextContent(HOME_URI) PaddedButton("Deeplink Away!", onClick = dropUnlessResumed { val intent = Intent( this@StaticUriDeepLinkActivity, MainActivity::class.java ) // the uri to deep link with intent.data = HOME_URI.toUri() startActivity(intent) }) } } } }
package com.example.nav3recipes.deeplink.handlerequests.staticuri import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.navigation3.runtime.NavBackStack import androidx.navigation3.runtime.NavKey import androidx.navigation3.runtime.entryProvider import androidx.navigation3.runtime.rememberNavBackStack import androidx.navigation3.ui.NavDisplay import com.example.nav3recipes.common.deeplink.EntryScreen import com.example.nav3recipes.common.deeplink.TextContent import com.example.nav3recipes.deeplink.handlerequests.uriarguments.HomeKey import com.example.nav3recipes.deeplink.handlerequests.uriarguments.NavRecipeKey import androidx.navigation3.runtime.deeplink.DeepLinkRequest import androidx.navigation3.runtime.deeplink.DeepLinkUri import androidx.navigation3.runtime.deeplink.UriDeepLinkMatcher import androidx.navigation3.runtime.deeplink.invoke import com.example.nav3recipes.ui.setEdgeToEdgeConfig import kotlinx.serialization.Serializable import kotlinx.serialization.serializer @Serializable internal object FallbackKey: NavRecipeKey { override val name: String = "Fallback Key" } class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { setEdgeToEdgeConfig() super.onCreate(savedInstanceState) // create a DeepLinkRequest with the intent val request = DeepLinkRequest(intent) // try to match DeepLinkRequest to a DeepLinkMatcher val matchResult = HOME_MATCHER.match(request) val key = matchResult?.key ?: FallbackKey /** * Then pass starting key to backstack */ setContent { val backStack: NavBackStack<NavKey> = rememberNavBackStack(key) NavDisplay( backStack = backStack, onBack = { backStack.removeLastOrNull() }, entryProvider = entryProvider { entry<HomeKey> { key -> EntryScreen(key.name) { TextContent("Deep linked to Home") } } entry<FallbackKey> { key -> EntryScreen("${key.name} ") { TextContent( "Failed to deep link - DeepLinkRequest " + "did not match with any DeepLinkMatcher" ) } } } ) } } } /** * Each matcher is associated with a navigation key that supports this deep link. * * A navigation key can be associated with multiple DeepLinkMatchers if it supports more than one deep link. */ private val HOME_MATCHER = UriDeepLinkMatcher( uriPattern = DeepLinkUri(HOME_URI), serializer = serializer<HomeKey>(), )