美文网首页
Navigation Architecture Componen

Navigation Architecture Componen

作者: 为什么要简称 | 来源:发表于2019-01-04 14:45 被阅读7次

11.Associating a web link with a destination

<deepLink>元素

deep link最常用的场景是允许一个web link打开app中的某个activity。Traditionally you would use an intent-filter and associate a URL with the activity you want to open.

navigation让这变得灰常简单,它允许在导航图中直接映射URL到destination中。

每个<deepLink>有个必要属性:app:uri。

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

  • uri如果没有scheme,那么会被认为是http://或者https://
  • 可以用占位符匹配1个或多个特征,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个或多个特征
  • NavController will automatically handle ACTION_VIEW intents and look for matching deep links.
Add a URIbased Deep Link using <deepLink>

1.mobile_navigation
2.对deeplink_dest添加<deepLink>元素

 <deepLink app:uri="www.example.com/{myarg}" />

3.AndroidManifest.xml
4.添加nav-graph标签,这样可以确保生成合适的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启动app,方法有两个:

  • 通过adb
adb shell am start -a android.intent.action.VIEW -d "http://www.example.com/urlTest" 
  • 通过search bar(Select Navigation codelab)
    我手上的机子都没有google的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

总结一下:

  • 导航图(Navigation graph structure)
  • NavHostFragment 和 NavController
  • 导航去指定的destination
  • 使用action进行导航
  • 在destination之前传递参数, 使用safeargs plugin
  • 通过menus, bottom navs, navigation drawers进行导航
  • 通过deep link进行导航

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

想看更多请移步 documentation. 其他绝世武功请点击 Architecture Components, 看看以下路数:

相关文章

网友评论

      本文标题:Navigation Architecture Componen

      本文链接:https://www.haomeiwen.com/subject/maehrqtx.html