11.Associating a web link with a destination
<deepLink>元素
deep link最常用的場(chǎng)景是允許一個(gè)web link打開(kāi)app中的某個(gè)activity接箫。Traditionally you would use an intent-filter and associate a URL with the activity you want to open.
navigation讓這變得灰常簡(jiǎn)單哟玷,它允許在導(dǎo)航圖中直接映射URL到destination中。
每個(gè)<deepLink>有個(gè)必要屬性:app:uri察郁。
除了直接使用URI匹配外崭庸,還支持一下屬性:
- uri如果沒(méi)有scheme美旧,那么會(huì)被認(rèn)為是http://或者h(yuǎn)ttps://
- 可以用占位符匹配1個(gè)或多個(gè)特征怀大,The String value of the placeholder is available in the arguments Bundle which has a key of the same name. For example,
http://www.example.com/users/{id}
will matchhttp://www.example.com/users/4
. - 可以使用通配符.*匹配0個(gè)或多個(gè)特征
- NavController will automatically handle ACTION_VIEW intents and look for matching deep links.
Add a URIbased Deep Link using <deepLink>
1.mobile_navigation
2.對(duì)deeplink_dest
添加<deepLink>元素
<deepLink app:uri="www.example.com/{myarg}" />
3.AndroidManifest.xml
4.添加nav-graph標(biāo)簽,這樣可以確保生成合適的intent filter。
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<nav-graph android:value="@navigation/mobile_navigation" />
</activity>
如果好奇到底生成了什么鬼精肃,在 APK Analyzer中打開(kāi)app-debug.apk秤涩,看看AndroidManifest.xml。
5.通過(guò)deep link啟動(dòng)app司抱,方法有兩個(gè):
- 通過(guò)adb
adb shell am start -a android.intent.action.VIEW -d "http://www.example.com/urlTest"
- 通過(guò)search bar(Select Navigation codelab)
我手上的機(jī)子都沒(méi)有g(shù)oogle的search bar了筐眷,所以沒(méi)有玩。
12.Try navigating on your own
This is a recap of the skills you've learned during this codelab. This step does not include comments, so try it on your own:
Create a new fragment class
Add the fragment as a destination to your navigation graph
Have the shopping cart icon open up your new fragment class, using NavigationUI to handle the menu.
13.Congratulations
總結(jié)一下:
- 導(dǎo)航圖(Navigation graph structure)
- NavHostFragment 和 NavController
- 導(dǎo)航去指定的destination
- 使用action進(jìn)行導(dǎo)航
- 在destination之前傳遞參數(shù), 使用safeargs plugin
- 通過(guò)menus, bottom navs, navigation drawers進(jìn)行導(dǎo)航
- 通過(guò)deep link進(jìn)行導(dǎo)航
You can continue to explore with this app or start using navigation in your own app.
There's a lot more to try, including:
- Popping destinations off the backstack (or any backstack manipulations)
- Nested navigation graphs
- Conditional navigation
- Adding support for new destinations
想看更多請(qǐng)移步 documentation. 其他絕世武功請(qǐng)點(diǎn)擊 Architecture Components, 看看以下路數(shù):
- Room with a View Codelab (LiveData, ViewModel and Room)
- Android WorkManager Codelab
- Android Paging Codelab
- Android Lifecycle-aware components Codelab (LiveData and ViewModel)
- Android Persistence Codelab (Room)