在目标之间进行导航的推荐方式是使用 Safe Args Gradle 插件。此插件生成对象和构建器类,可实现目标之间的类型安全导航。使用 Safe Args 进行导航和在目标之间传递数据。
启用 Safe Args
要将 Safe Args 添加到您的项目,请在您的顶级 build.gradle
文件中包含以下 classpath
Groovy
buildscript { repositories { google() } dependencies { def nav_version = "2.9.0" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } }
Kotlin
buildscript { repositories { google() } dependencies { val nav_version = "2.9.0" classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version") } }
您还必须应用两个可用插件之一。
要生成适用于 Java 或混合 Java 和 Kotlin 模块的 Java 语言代码,请将此行添加到您的应用或模块的 build.gradle
文件中
Groovy
plugins { id 'androidx.navigation.safeargs' }
Kotlin
plugins { id("androidx.navigation.safeargs") }
或者,要生成适用于纯 Kotlin 模块的 Kotlin 代码,请添加
Groovy
plugins { id 'androidx.navigation.safeargs.kotlin' }
Kotlin
plugins { id("androidx.navigation.safeargs.kotlin") }
您必须在gradle.properties
文件中包含 android.useAndroidX=true
,具体请参阅迁移到 AndroidX。
生成的代码
启用 Safe Args 后,您生成的代码会包含针对您定义的每个操作的类和方法,以及与每个发送和接收目标对应的类。
Safe Args 为每个操作源自的目标生成一个类。生成的类名会在源目标类名后面添加 "Directions"。例如,如果源目标的名称为 SpecifyAmountFragment
,则生成的类名为 SpecifyAmountFragmentDirections
。
生成的类包含一个静态方法,用于源目标中定义的每个操作。此方法会将所有定义的操作参数作为参数,并返回一个 NavDirections
对象,您可以直接将其传递给 navigate()
。
Safe Args 示例
例如,考虑一个导航图,其中包含一个连接两个目标(SpecifyAmountFragment
和 ConfirmationFragment
)的单个操作。ConfirmationFragment
接受一个 float
参数,您将其作为操作的一部分提供。
Safe Args 生成一个 SpecifyAmountFragmentDirections
类,其中包含一个方法 actionSpecifyAmountFragmentToConfirmationFragment()
和一个名为 ActionSpecifyAmountFragmentToConfirmationFragment
的内部类。该内部类派生自 NavDirections
,并存储关联的操作 ID 和 float
参数。然后,可以将返回的 NavDirections
对象直接传递给 navigate()
,如以下示例所示
Kotlin
override fun onClick(v: View) {
val amount: Float = ...
val action =
SpecifyAmountFragmentDirections
.actionSpecifyAmountFragmentToConfirmationFragment(amount)
v.findNavController().navigate(action)
}
Java
@Override
public void onClick(View view) {
float amount = ...;
action =
SpecifyAmountFragmentDirections
.actionSpecifyAmountFragmentToConfirmationFragment(amount);
Navigation.findNavController(view).navigate(action);
}
有关使用 Safe Args 在目标之间传递数据的更多信息,请参阅使用 Safe Args 以类型安全的方式传递数据。
使用 Safe Args 确保类型安全
使用 Safe Args Gradle 插件在目标之间进行导航。此插件生成简单的对象和构建器类,可实现目标之间的类型安全导航和参数传递。
要将 Safe Args 添加到您的项目,请在您的顶级 build.gradle
文件中包含以下 classpath
Groovy
buildscript { repositories { google() } dependencies { def nav_version = "2.9.0" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } }
Kotlin
buildscript { repositories { google() } dependencies { val nav_version = "2.9.0" classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version") } }
您还必须应用两个可用插件之一。
要生成适用于 Java 或混合 Java 和 Kotlin 模块的 Java 语言代码,请将此行添加到您的应用或模块的 build.gradle
文件中
Groovy
plugins { id 'androidx.navigation.safeargs' }
Kotlin
plugins { id("androidx.navigation.safeargs") }
或者,要生成适用于纯 Kotlin 模块的 Kotlin 代码,请添加
Groovy
plugins { id 'androidx.navigation.safeargs.kotlin' }
Kotlin
plugins { id("androidx.navigation.safeargs.kotlin") }
您必须在gradle.properties
文件中包含 android.useAndroidX=true
,具体请参阅迁移到 AndroidX。
启用 Safe Args 后,该插件会生成代码,其中包含针对您定义的每个操作的类和方法。对于每个操作,Safe Args 还会为每个*源目标*(即操作源自的目标)生成一个类。生成的类名是源目标类名与“Directions”一词的组合。例如,如果目标的名称为 SpecifyAmountFragment
,则生成的类名为 SpecifyAmountFragmentDirections
。生成的类包含一个静态方法,用于源目标中定义的每个操作。此方法会将所有定义的动作参数作为参数,并返回一个 NavDirections
对象,您可以将其传递给 navigate()
。
举例来说,假设我们有一个导航图,其中包含一个将源目标 SpecifyAmountFragment
连接到接收目标 ConfirmationFragment
的单个操作。
Safe Args 生成一个 SpecifyAmountFragmentDirections
类,其中包含一个方法 actionSpecifyAmountFragmentToConfirmationFragment()
,该方法返回一个 NavDirections
对象。然后,可以将返回的 NavDirections
对象直接传递给 navigate()
,如以下示例所示
Kotlin
override fun onClick(view: View) { val action = SpecifyAmountFragmentDirections .actionSpecifyAmountFragmentToConfirmationFragment() view.findNavController().navigate(action) }
Java
@Override public void onClick(View view) { NavDirections action = SpecifyAmountFragmentDirections .actionSpecifyAmountFragmentToConfirmationFragment(); Navigation.findNavController(view).navigate(action); }
有关使用 Safe Args 在目标之间传递数据的更多信息,请参阅“在目标之间传递数据”中的使用 Safe Args 以类型安全的方式传递数据。