Mulai versi 1.2.0, Navigation 3 mendukung deep linking ke
tujuan di aplikasi Anda menggunakan class DeepLinkRequest dan
DeepLinkMatcher.
Untuk mendukung deep link di aplikasi Anda, selesaikan langkah-langkah berikut:
- Tentukan filter intent di
AndroidManifest.xmluntuk menentukan URI yang dapat ditangani aplikasi Anda. - Buat instance
DeepLinkMatcheruntuk memetakan permintaan masuk ke kunci navigasi Anda. - Cocokkan permintaan masuk dalam metode
onCreateatauonNewIntentaktivitas Anda dan perbarui tumpukan kembali Anda.
Membuat DeepLinkRequest
DeepLinkRequest mewakili deep link masuk. Link ini berisi
DeepLinkUri dan RequestExtras opsional dengan informasi tambahan, seperti tindakan intent atau jenis MIME.
// 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") } )
Menyediakan tambahan DeepLinkRequest
Untuk menyimpan informasi tambahan terkait deep link, gunakan class
RequestExtras. Untuk membuat penentuan dan pembuatan instance tambahan yang aman,
library ini menyediakan antarmuka RequestExtrasKey dan
requestExtras DSL.
Selain itu, library ini menyediakan dua kunci tambahan dan helper terkait:
MimeTypeExtrasKey: Digunakan untuk menyimpanStringjenis MIME.ActionExtrasKey(khusus Android): Digunakan untuk menyimpanStringtindakanIntent.
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)
Untuk menentukan tambahan kustom Anda sendiri, implementasikan antarmuka RequestExtrasKey
dengan generik yang diketik:
// Define a custom typed key: object CampaignIdExtrasKey : RequestExtrasKey<String> val customExtras: RequestExtras = requestExtras { put(CampaignIdExtrasKey, "123") } val campaignId: String? = customExtras[CampaignIdExtrasKey]
Anda juga dapat menggunakan emptyRequestExtras() untuk membuat instance kosong, atau
menggabungkan tambahan menggunakan operator + (plus) dan - (minus).
Membuat DeepLinkRequest dari Intent
Di Android, Anda dapat membuat DeepLinkRequest langsung dari Intent. Jika dibuat dengan cara ini, DeepLinkRequest akan dibuat sebagai berikut:
uridisalin dari kolomdataintent.- Jika tidak null, jenis MIME dan tambahan tindakan akan ditetapkan dari kolom intent yang sesuai.
- Semua
intent.extrasdengan nilai non-null disimpan sebagaiSavedStatediDeepLinkRequest.IntentExtrasKey. - Tambahan lainnya yang diberikan menggunakan parameter
extrasakan ditambahkan.
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"
Membuat instance DeepLinkMatcher
A DeepLinkMatcher memetakan instance DeepLinkRequest masuk ke
kunci navigasi yang dapat ditambahkan ke tumpukan kembali aplikasi Anda. Navigation 3
menyediakan tiga pencocok bawaan: UriDeepLinkMatcher untuk
pencocokan URI berbasis pola, StaticKeyDeepLinkMatcher untuk link dasar, dan
BackStackMatcher untuk membuat tumpukan kembali sintetis. Library ini juga
mendukung pencocok kustom untuk kasus penggunaan yang tidak tercakup oleh pencocok bawaan.
Untuk mengetahui informasi selengkapnya, lihat Membuat DeepLinkMatchers.
Menambahkan filter intent
Untuk mengaktifkan deep link untuk memulai aktivitas Anda, Anda harus menentukan elemen yang cocok
<intent-filter> di AndroidManifest.xml aplikasi Anda. Untuk mengetahui informasi selengkapnya, lihat Menambahkan filter intent untuk link masuk.
Mencocokkan permintaan masuk
Setelah Anda membuat instance DeepLinkMatcher Anda, Anda dapat mencocokkan
permintaan masuk di aktivitas Anda.
Untuk mencocokkan permintaan masuk, selesaikan langkah-langkah berikut:
- Buat instance
DeepLinkMatcherAnda. - Kumpulkan semua instance
DeepLinkMatcherAnda, baik secara eksplisit maupun menggunakan multibinding. - Buat
DeepLinkRequestdariIntentmasuk. - Cocokkan permintaan dengan semua pencocok dan temukan kecocokan terbaik.
- Buat tumpukan kembali dari hasil kecocokan.
// 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) } } }