由於官網暫時沒有中文版本,所以在這裏根據內容抽空做了一些中文的版本.
原文首發於 https://leonchen1024.com/2019/07/04/Navigation/#more
Navigation
是用來管理你的app中內容的跳轉,返回等操作.它主要包含了以下三個部分:
Navigation graph
: 一個 xml 資源包含了所有的導航相關的信息. 這些信息包含了你app裏所有獨立的內容區域,也就是destinations
.NavHost
: 一個空白的容器用來展示你的Navigation graph
中的destinations
.Navigation
模塊中包含了一個默認的實現NavHostFragment
用來展示 Fragment 的destinations
.NavController
: 一個管理 NavHost 中的導航的對象. 會在用戶使用app的時候管理 NavHost 中的內容.
當在頁面內導航的時候,你可以告訴 NavController 你想要通過Navigation graph 中的某個路徑或者直接導航到一個destination .這時候 NavController 就會在 NavHost 中展示合適的 destination.
Navigation 模塊還提供了其他的好處,比如:
- 處理了 fragment transactions
- 默認情況下正確的處理了向上和返回動作
- 爲動畫和過渡提供了標準化的資源
- 實現和處理了深鏈接
- 內置了 Navigation UI 組件,比如 navigation drawers 和 bottom navigation,可以減少附加的工作
- Safe Args - 一個 Gradle 插件當導航並在兩個目的地中傳遞數據的時候提供了類型安全機制
- ViewModel 支持 - 你可以將 ViewModel 的作用域設置爲 navigation graph 來在不同的 目的地之間共享 UI 相關連的數據
並且你還可以使用 Android Studio’s Navigation Editor 來查看和編輯 navigation graphs.
原文首發於 https://leonchen1024.com/2019/07/04/Navigation/#more
Next steps
For more documentation and resources related to the Navigation component, see the following.
Getting started
Further topics
- Principles of Navigation
- Design for different form factors
- Design navigation graphs
- Nested graphs
- Global actions
- Conditional navigation
- Pass data between destinations
- Create a deep link for a destination
- Animate transitions between destinations
- Update UI components with NavigationUI
- Create swipe views with tabs
- Interact programmatically with the Navigation component
- Test navigation
- Add new destination types
- Provide custom back navigation
- Migrate to the Navigation component
Additional resources
To learn more about navigation, see the following additional resources.
Samples
Codelabs
Blog posts
Videos
- 10 Best Practices for Moving to a Single Activity
- Single Activity: Why, When, and How (Android Dev Summit '18)
- Android Jetpack: manage UI navigation with Navigation Controller (Google I/O '18)
Reference
https://developer.android.google.cn/guide/navigation
About Me
我的博客 leonchen1024.com
我的 GitHub https://github.com/LeonChen1024
微信公衆號