Navigation Architecture Component 4

11.Associating a web link with a destination

元素

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中。

每个有个必要属性: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

1.mobile_navigation
2.对deeplink_dest添加元素

 

3.AndroidManifest.xml
4.添加nav-graph标签,这样可以确保生成合适的intent filter。

    
        
            
            
            
        
        
    

如果好奇到底生成了什么鬼,在 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, 看看以下路数:

  • 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)

你可能感兴趣的:(Navigation Architecture Component 4)