Navigation Architecture Component 4

11.Associating a web link with a destination

<deepLink>元素

deep link最常用的場(chǎng)景是允許一個(gè)web link打開app中的某個(gè)activity。Traditionally you would use an intent-filter and associate a URL with the activity you want to open.

navigation讓這變得灰常簡單,它允許在導(dǎo)航圖中直接映射URL到destination中。

每個(gè)<deepLink>有個(gè)必要屬性:app:uri。

除了直接使用URI匹配外,還支持一下屬性:

  • uri如果沒有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 match http://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中打開app-debug.apk,看看AndroidManifest.xml。
5.通過deep link啟動(dòng)app,方法有兩個(gè):

  • 通過adb
adb shell am start -a android.intent.action.VIEW -d "http://www.example.com/urlTest" 
  • 通過search bar(Select Navigation codelab)
    我手上的機(jī)子都沒有g(shù)oogle的search bar了,所以沒有玩。

12.Try navigating on your own

image.png

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
  • 通過menus, bottom navs, navigation drawers進(jìn)行導(dǎo)航
  • 通過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ù):

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容