Navigation Architecture Componen

2019-01-04  本文已影响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匹配外,还支持一下属性:

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 shell am start -a android.intent.action.VIEW -d "http://www.example.com/urlTest" 

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

总结一下:

You can continue to explore with this app or start using navigation in your own app.

There's a lot more to try, including:

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

上一篇 下一篇

猜你喜欢

热点阅读