1.2.0 sürümünden itibaren Navigation 3, DeepLinkRequest ve DeepLinkMatcher sınıflarını kullanarak uygulamanızdaki hedeflere derin bağlantı oluşturmayı destekler.
Uygulamanızda derin bağlantıları desteklemek için aşağıdaki adımları tamamlayın:
- Uygulamanızın hangi URI'leri işleyebileceğini belirtmek için
AndroidManifest.xmldosyanızda intent filtreleri tanımlayın. - Gelen istekleri gezinme tuşlarınızla eşlemek için
DeepLinkMatcherörnek oluşturun. - Etkinliğinizin
onCreateveyaonNewIntentyönteminde gelen istekleri eşleştirin ve eski yığınınızı buna göre güncelleyin.
DeepLinkRequest oluşturun
DeepLinkRequest, gelen bir derin bağlantıyı temsil eder. Bir DeepLinkUri ve intent işlemi veya MIME türü gibi ek bilgiler içeren isteğe bağlı bir RequestExtras içerir.
// 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 ekstrası sunun
Derin bağlantıyla ilgili ek bilgileri depolamak için RequestExtras sınıfını kullanın. Kitaplık, ekstraları tanımlamayı ve örneklendirmeyi tip güvenli hale getirmek için RequestExtrasKey arayüzünü ve requestExtras DSL'yi sağlar.
Ayrıca kitaplık, iki ek anahtar ve ilişkili yardımcılar sağlar:
MimeTypeExtrasKey: MIME türünüStringdepolamak için kullanılır.ActionExtrasKey(Yalnızca Android):Intentişlemini depolamak için kullanılırString.
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)
Kendi özel ekstralarınızı tanımlamak için RequestExtrasKey arayüzünü türü belirtilmiş bir genel türle uygulayın:
// Define a custom typed key: object CampaignIdExtrasKey : RequestExtrasKey<String> val customExtras: RequestExtras = requestExtras { put(CampaignIdExtrasKey, "123") } val campaignId: String? = customExtras[CampaignIdExtrasKey]
Boş bir örnek oluşturmak için emptyRequestExtras() yöntemini de kullanabilir veya + (plus) ve - (minus) operatörlerini kullanarak ekstraları birleştirebilirsiniz.
Intent parametresinden DeepLinkRequest oluşturun
Android'de doğrudan Intent üzerinden DeepLinkRequest oluşturabilirsiniz. Bu şekilde oluşturulduğunda DeepLinkRequest şu şekilde oluşturulur:
uri, amacındataalanından kopyalanır.- Boş değilse MIME türü ve işlem ekstraları, ilgili intent alanlarından ayarlanır.
- Boş olmayan değerlere sahip tüm
intent.extras,DeepLinkRequest.IntentExtrasKeyiçindeSavedStateolarak kaydedilir. extrasparametresi kullanılarak sağlanan ekstralar eklenir.
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 örnek oluşturun
DeepLinkMatcher, gelen DeepLinkRequest örneklerini uygulamanızın eski yığınına eklenebilecek gezinme tuşlarıyla eşler. Navigation 3, üç yerleşik eşleştirici sunar: kalıp tabanlı URI eşleştirme için UriDeepLinkMatcher, temel bağlantılar için StaticKeyDeepLinkMatcher ve sentetik eski yığınlar oluşturmak için BackStackMatcher. Kitaplık, yerleşik eşleştiricilerin kapsamadığı kullanım alanları için özel eşleştiricileri de destekler.
Daha fazla bilgi için DeepLinkMatcher oluşturma başlıklı makaleyi inceleyin.
Intent filtreleri ekleme
Etkinliğinizi başlatmak için bir derin bağlantıyı etkinleştirmek istiyorsanız uygulamanızın AndroidManifest.xml dosyasında eşleşen <intent-filter> öğelerini tanımlamanız gerekir. Daha fazla bilgi için Gelen bağlantılar için intent filtreleri ekleme başlıklı makaleyi inceleyin.
Gelen bir isteği eşleştirme
DeepLinkMatcher örneklerinizi oluşturduktan sonra etkinliğinizdeki gelen istekleri eşleştirebilirsiniz.
Gelen bir isteği eşleştirmek için aşağıdaki adımları uygulayın:
DeepLinkMatcherörneklerinizi örneklendirin.- Açıkça veya çoklu bağlamaları kullanarak tüm
DeepLinkMatcherörneklerinizi harmanlayın. - Gelen
IntentüzerindenDeepLinkRequestoluşturun. - İsteği tüm eşleştiricilerle eşleştirir ve en iyi eşleşmeyi bulur.
- Eşleşme sonucundan eski yığını oluşturun.
// 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) } } }