导航是指允许用户跨越、进入和退出应用中不同内容片段的交互。
Android Jetpack 的 Navigation 组件包含 Navigation 库、Safe Args Gradle 插件,以及可帮助您实现应用导航的工具。Navigation 组件可处理各种导航用例,从简单的按钮点击到更复杂的模式(例如应用栏和抽屉式导航栏)。
主要概念
下表简要介绍了导航中的关键概念,以及用于实现这些概念的主要类型。
构思 |
目的 |
类型 |
---|---|---|
宿主 |
包含当前导航目的地的界面元素。也就是说,当用户浏览应用时,该应用实际上会在导航宿主中切换目的地。 |
|
图表 |
一种数据结构,用于定义应用中的所有导航目的地以及它们如何连接在一起。 |
|
控制器 |
用于管理目的地之间导航的中央协调器。该控制器提供了一些方法,可在目的地之间导航、处理深层链接、管理返回堆栈等。 |
|
目的地 |
导航图中的节点。当用户导航到此节点时,主机会显示其内容。 |
通常是在构建导航图时创建的。 |
路线 |
唯一标识目的地及其所需的任何数据。 您可以使用路线导航。路线可带您前往目的地。 |
任何可序列化的数据类型。 |
优点和功能
Navigation 组件提供了许多其他优势和功能,包括:
- 动画和过渡:为动画和过渡提供标准化资源。
- 深层链接:实现并处理可将用户直接转到目的地的深层链接。
- 界面模式:只需极少的额外工作即可支持抽屉式导航栏和底部导航等模式。
- 类型安全:支持在目的地之间传递类型安全的数据。
- ViewModel 支持:允许将
ViewModel
的作用域限定为导航图,以便在导航图的目的地之间共享与界面相关的数据。 - fragment 事务:全面支持和处理 fragment 事务。
- 返回和向上:默认情况下,系统会正确处理返回和向上操作。
设置您的环境
如需在您的项目中添加导航支持,请向应用的 build.gradle
文件添加以下依赖项:
plugins { // Kotlin serialization plugin for type safe routes and navigation arguments id 'org.jetbrains.kotlin.plugin.serialization' version '2.0.21' } dependencies { def nav_version = "2.8.9" // Jetpack Compose Integration implementation "androidx.navigation:navigation-compose:$nav_version" // Views/Fragments Integration implementation "androidx.navigation:navigation-fragment:$nav_version" implementation "androidx.navigation:navigation-ui:$nav_version" // Feature module support for Fragments implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version" // Testing Navigation androidTestImplementation "androidx.navigation:navigation-testing:$nav_version" // JSON serialization library, works with the Kotlin serialization plugin. implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3" }
plugins { // Kotlin serialization plugin for type safe routes and navigation arguments kotlin("plugin.serialization") version "2.0.21" } dependencies { val nav_version = "2.8.9" // Jetpack Compose integration implementation("androidx.navigation:navigation-compose:$nav_version") // Views/Fragments integration implementation("androidx.navigation:navigation-fragment:$nav_version") implementation("androidx.navigation:navigation-ui:$nav_version") // Feature module support for Fragments implementation("androidx.navigation:navigation-dynamic-features-fragment:$nav_version") // Testing Navigation androidTestImplementation("androidx.navigation:navigation-testing:$nav_version") // JSON serialization library, works with the Kotlin serialization plugin implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3") }
如需了解如何向您的项目添加其他架构组件,请参阅向项目添加组件。
后续步骤
有关 Navigation 组件的更多文档和资源,请参阅以下资源。
详细指南
如需详细了解如何实现导航宿主和 NavController
,以及它们如何与 Compose 和其他界面框架进行交互,请参阅以下指南:
Codelab
视频
- 浏览导航组件
- 移到一个 activity 的 10 大最佳实践
- 一个 activity:为何、何时以及如何(2018 年 Android 开发者峰会)
- Android Jetpack:使用导航控制器管理界面导航(2018 年 Google I/O 大会)
示例
Jetsnack is a sample snack ordering app built with Jetpack Compose.
To try out this sample app, use the latest stable version of Android Studio. You can clone this repository or import the project from Android Studio following the steps here.
This Jetcaster is a sample podcast app, built with Jetpack Compose. The goal of the sample is to showcase building with Compose across multiple form factors (mobile, TV, and Wear) and full featured architecture.
To try out this sample app, use the latest Jetchat is a sample chat app built with Jetpack Compose.
To try out this sample app, use the latest stable version of Android Studio. You can clone this repository or import the project from Android Studio following the steps here.
This sample These samples showcase different architectural approaches to developing Android apps. In its different branches you'll find the same app (a TODO app) implemented with small differences.
In this branch you'll find:
User Interface built with Jetpack Jetnews is a sample news reading app, built with Jetpack Compose. The goal of the sample is to showcase the current UI capabilities of Compose.
To try out this sample app, use the latest stable version of Android Studio. You can clone this repository Learn how this app was designed and built in the design case study, architecture learning journey and modularization learning journey.
This is the repository for the Now in Android app. It is a work in progress 🚧.
Now in Android is a fully functionalJetsnack sample
Jetcaster sample 🎙️
Jetchat sample
Architecture
Jetnews sample
Now in Android App