Navigation 3, 1.2.0 sürümünden itibaren 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örnekleri oluşturun. - Etkinliğinizin
onCreateveyaonNewIntentyönteminde gelen istekleri eşleştirin ve geri yığını buna göre güncelleyin.
DeepLinkRequest oluşturun
DeepLinkRequest, gelen bir derin bağlantıyı temsil eder. Bir DeepLinkUri ve isteğe bağlı RequestExtras ile birlikte intent işlemi veya MIME türü gibi ek bilgiler 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 ekstraları sunma
Derin bağlantıyla ilgili ek bilgileri depolamak için RequestExtras sınıfını kullanın. Kitaplık, ekstraları tür güvenli bir şekilde tanımlayıp örneklemek 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: Bir MIME türünüStringdepolamak için kullanılır.ActionExtrasKey(yalnızca Android):Intentkullanıcısının işlemini saklamak 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 öğeyle 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()'ı da kullanabilir veya + (plus) ve - (minus) operatörlerini kullanarak ekstraları birleştirebilirsiniz.
Intent öğesinden DeepLinkRequest oluşturma
Android'de, Intent üzerinden doğrudan DeepLinkRequest oluşturabilirsiniz. Bu şekilde oluşturulduğunda DeepLinkRequest şu şekilde oluşturulur:
uri, niyetindataalanından kopyalanır.- Boş değilse MIME türü ve işlem ekstraları, ilgili amaç 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 örnekleri oluşturma
DeepLinkMatcher, gelen DeepLinkRequest örneklerini, uygulamanızın geri yığınına eklenebilen gezinme tuşlarıyla eşler. Navigation 3
üç yerleşik eşleştirici sağlar: UriDeepLinkMatcher, kalıba dayalı URI eşleştirme için, StaticKeyDeepLinkMatcher temel bağlantılar için ve BackStackMatcher sentetik geri yığınları oluşturmak için. Kitaplık, yerleşik eşleştiricilerin kapsamadığı kullanım alanları için özel eşleştiricileri de destekler.
Daha fazla bilgi için Create DeepLinkMatchers başlıklı makaleyi inceleyin.
Amaç filtreleri ekleme
Etkinliğinizi başlatmak için derin bağlantıyı etkinleştirmek üzere uygulamanızın AndroidManifest.xml bölümünde eşleşen <intent-filter> öğelerini tanımlamanız gerekir. Daha fazla bilgi için Gelen bağlantılar için amaç filtreleri ekleme başlıklı makaleyi inceleyin.
Gelen 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ı tamamlayın:
DeepLinkMatcherörneklerinizi oluşturun.DeepLinkMatcherörneklerinizin tümünü açıkça veya çoklu bağlamalar kullanarak bir araya getirin.- Gelen
IntentiletilerdenDeepLinkRequestoluşturun. - İsteği tüm eşleştiricilerle eşleştirin ve en iyi eşleşmeyi bulun.
- Maç sonucundan geri 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) } } }