Navigation 指南

文章目錄


由於官網暫時沒有中文版本,所以在這裏根據內容抽空做了一些中文的版本.

原文首發於 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

Additional resources

To learn more about navigation, see the following additional resources.

Samples

Codelabs

Blog posts

Videos

Reference

https://developer.android.google.cn/guide/navigation

About Me

我的博客 leonchen1024.com

我的 GitHub https://github.com/LeonChen1024

微信公衆號

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章