वर्शन 1.2.0 से, Navigation 3 में DeepLinkRequest और DeepLinkMatcher क्लास का इस्तेमाल करके, आपके ऐप्लिकेशन में मौजूद डेस्टिनेशन के लिए डीप लिंकिंग की सुविधा काम करती है.
अपने ऐप्लिकेशन में डीप लिंक की सुविधा चालू करने के लिए, यह तरीका अपनाएं:
- इंटेंट फ़िल्टर तय करें: अपने
AndroidManifest.xmlमें इंटेंट फ़िल्टर तय करें, ताकि यह बताया जा सके कि आपका ऐप्लिकेशन किन यूआरआई को हैंडल कर सकता है. DeepLinkMatcherइंस्टेंस बनाएं, ताकि आने वाले अनुरोधों को नेविगेशन बटन पर मैप किया जा सके.- अपनी गतिविधि के
onCreateयाonNewIntentतरीके में, इनकमिंग अनुरोधों को मैच करें और उसके हिसाब से अपनी पिछली गतिविधियों को अपडेट करें.
DeepLinkRequest बनाएं
DeepLinkRequest, इनकमिंग डीप लिंक को दिखाता है. इसमें एक DeepLinkUri और वैकल्पिक RequestExtras होता है. इसमें अतिरिक्त जानकारी भी होती है, जैसे कि इंटेंट ऐक्शन या एमआईएमई टाइप.
// Create a request with a String URI val request = DeepLinkRequest(uri = "https://www.example.com/home") // Create a request from a DeepLinkUri val deepLinkUri = DeepLinkUri("https://www.example.com/home") val requestFromUri = DeepLinkRequest(uri = deepLinkUri) // Create a request with a URI and action val requestWithAction = DeepLinkRequest( uri = "https://www.example.com/home", extras = DeepLinkRequest.actionExtra("android.intent.action.VIEW") ) // Create a request with URI, action and mimeType val requestWithMimeType = DeepLinkRequest( uri = "https://www.example.com/image", extras = requestExtras { put(DeepLinkRequest.ActionExtrasKey, "android.intent.action.VIEW") put(DeepLinkRequest.Companion.MimeTypeExtrasKey, "image/png") } )
DeepLinkRequest अतिरिक्त सुविधाएं दें
डीप लिंक से जुड़ी अतिरिक्त जानकारी सेव करने के लिए, RequestExtras क्लास का इस्तेमाल करें. एक्स्ट्रा को टाइप-सेफ़ तरीके से तय करने और इंस्टैंशिएट करने के लिए, लाइब्रेरी में RequestExtrasKey इंटरफ़ेस और requestExtras डीएसएल उपलब्ध है.
इसके अलावा, लाइब्रेरी में दो अतिरिक्त कुंजियां और उनसे जुड़े हेल्पर होते हैं:
MimeTypeExtrasKey: इसका इस्तेमाल, एमआईएमई टाइपStringको स्टोर करने के लिए किया जाता है.ActionExtrasKey(सिर्फ़ Android के लिए): इसका इस्तेमाल,Intentकी कार्रवाई को सेव करने के लिए किया जाता हैString.
val extras: RequestExtras = requestExtras { put(DeepLinkRequest.Companion.MimeTypeExtrasKey, "application/json") put(DeepLinkRequest.ActionExtrasKey, Intent.ACTION_VIEW) } // Access typed values using the get operator val mimeType: String? = extras[DeepLinkRequest.Companion.MimeTypeExtrasKey] val action: String? = extras[DeepLinkRequest.ActionExtrasKey] // Create extras using helper functions and combine them val mimeTypeExtras: RequestExtras = DeepLinkRequest.mimeTypeExtra("application/json") val combinedExtras: RequestExtras = extras + DeepLinkRequest.actionExtra(Intent.ACTION_VIEW)
अपने कस्टम एक्स्ट्रा तय करने के लिए, टाइप किए गए जेनरिक के साथ RequestExtrasKey इंटरफ़ेस लागू करें:
// Define a custom typed key: object CampaignIdExtrasKey : RequestExtrasKey<String> val customExtras: RequestExtras = requestExtras { put(CampaignIdExtrasKey, "123") } val campaignId: String? = customExtras[CampaignIdExtrasKey]
खाली इंस्टेंस बनाने के लिए, emptyRequestExtras() का भी इस्तेमाल किया जा सकता है. इसके अलावा, + (plus) और - (minus) ऑपरेटर का इस्तेमाल करके, अतिरिक्त चीज़ों को जोड़ा जा सकता है.
Intent से DeepLinkRequest बनाना
Android पर, Intent से सीधे DeepLinkRequest बनाया जा सकता है. इस तरह से बनाए जाने पर, DeepLinkRequest को इस तरह बनाया जाता है:
uriको इंटेंट केdataफ़ील्ड से कॉपी किया जाता है.- अगर वैल्यू शून्य नहीं है, तो एमआईएमई टाइप और कार्रवाई के एक्स्ट्रा, उनसे जुड़े इंटेंट फ़ील्ड से सेट किए जाते हैं.
- नॉन-नुल वैल्यू वाले सभी
intent.extras,DeepLinkRequest.IntentExtrasKeyमेंSavedStateके तौर पर सेव किए जाते हैं. extrasपैरामीटर का इस्तेमाल करके दी गई कोई भी अतिरिक्त जानकारी जोड़ दी जाती है.
object CampaignIdExtrasKey : RequestExtrasKey<String> val intent = Intent(Intent.ACTION_VIEW).apply { data = Uri.parse("https://www.example.com/item/42") type = "application/json" putExtra("user_id", "123") } val request = DeepLinkRequest( intent = intent, extras = requestExtras { put(CampaignIdExtrasKey, "spring_promo") } ) // The resulting DeepLinkRequest contains: val uri = request.uri // "https://www.example.com/item/42" val action = request.extras[DeepLinkRequest.ActionExtrasKey] // "android.intent.action.VIEW" val mimeType = request.extras[DeepLinkRequest.Companion.MimeTypeExtrasKey] // "application/json" val intentExtras: SavedState? = request.extras[DeepLinkRequest.IntentExtrasKey] val userId: String? = intentExtras?.read { getStringOrNull("user_id") } // "123" val campaignId: String? = request.extras[CampaignIdExtrasKey] // "spring_promo"
DeepLinkMatcher इंस्टेंस बनाएं
DeepLinkMatcher, आने वाले DeepLinkRequest इंस्टेंस को नेविगेशन बटन पर मैप करता है. इन बटन को आपके ऐप्लिकेशन की पिछली गतिविधियों में जोड़ा जा सकता है. Navigation 3 में, पहले से मौजूद तीन मैचर होते हैं: पैटर्न पर आधारित यूआरआई मैचिंग के लिए UriDeepLinkMatcher, बुनियादी लिंक के लिए StaticKeyDeepLinkMatcher, और सिंथेटिक बैक स्टैक बनाने के लिए BackStackMatcher. यह लाइब्रेरी, कस्टम मैचर के साथ भी काम करती है. इनका इस्तेमाल उन मामलों में किया जाता है जो पहले से मौजूद मैचर के दायरे में नहीं आते.
ज़्यादा जानकारी के लिए, DeepLinkMatchers बनाना लेख पढ़ें.
इंटेंट फ़िल्टर जोड़ना
अपनी गतिविधि शुरू करने के लिए डीप लिंक चालू करना ज़रूरी है. इसके लिए, आपको अपने ऐप्लिकेशन के AndroidManifest.xml में, मैच होने वाले <intent-filter> एलिमेंट तय करने होंगे. ज़्यादा जानकारी के लिए, इनकमिंग लिंक के लिए इंटेंट फ़िल्टर जोड़ना लेख पढ़ें.
आने वाले अनुरोध को मैच करना
DeepLinkMatcher इंस्टेंस बनाने के बाद, अपनी गतिविधि में आने वाले अनुरोधों को मैच किया जा सकता है.
आने वाले अनुरोध को मैच करने के लिए, यह तरीका अपनाएं:
DeepLinkMatcherइंस्टेंस को इंस्टैंशिएट करें.DeepLinkMatcherके सभी इंस्टेंस को एक साथ जोड़ें. इसके लिए, साफ़ तौर पर या मल्टीबाइंडिंग का इस्तेमाल करें.DeepLinkRequestबनाएं, जो आने वालेIntentसे ट्रिगर हो.- अनुरोध को सभी मैच करने वाले टूल से मैच करें और सबसे सही मैच ढूंढें.
- मैच के नतीजे से पिछली गतिविधियां बनाएं.
// 1. Instantiate your DeepLinkMatcher instances. val homeMatcher = StaticKeyDeepLinkMatcher(HomeKey, listOf(DeepLinkMatcher.actionFilter(Intent.ACTION_VIEW))) val userProfileMatcher = UriDeepLinkMatcher( DeepLinkUri("www.example.com/users/{id}"), serializer<UserProfileKey>() ).withBackStack { matchResult -> listOf(HomeKey, matchResult.key) } val telMatcher = TelDeepLinkMatcher() // 2. Collate all of your DeepLinkMatcher instances. // Note: Collating matchers with different generic types requires wildcards, // erasing the specific generic types. val deepLinkMatchers: List<DeepLinkMatcher<*, *>> = listOf( homeMatcher, userProfileMatcher, telMatcher ) class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // ... // 3. Create a DeepLinkRequest from the incoming Intent val request = DeepLinkRequest(intent = intent) // 4. Match the request against all matchers and find the best match. // Because DeepLinkMatcher.MatchResult implements Comparable, you can // use maxOrNull() to find the best match. val matchResult = deepLinkMatchers .mapNotNull { it.match(request) } // List<DeepLinkMatcher.MatchResult<*>> .maxOrNull() // DeepLinkMatcher.MatchResult<*>? // 5. Create the back stack from the match result (or fall back to a default). val backStack: List<NavKey> = when (matchResult) { // If no match is found, use the default back stack (e.g., HomeKey) null -> listOf(HomeKey) // If a BackStackMatchResult is found, use the back stack from the result is BackStackMatchResult<*, *> -> { // Because star-projected matchers erase the key type, cast the back stack to List<NavKey>. @Suppress("UNCHECKED_CAST") matchResult.backStack as List<NavKey> } // Otherwise, use the key from the match result to make a single-item back stack else -> listOf(matchResult.key as NavKey) } } }