如需在目的地之间导航,建议使用 Safe Args Gradle 插件。此插件可生成对象和构建器类,以便在目的地之间实现类型安全的导航。请使用 Safe Args 在目的地之间传递数据以及导航。
启用 Safe Args
To add Safe Args
to your project, include the following classpath
in your top level build.gradle
file:
Groovy
buildscript { repositories { google() } dependencies { def nav_version = "2.9.3" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } }
Kotlin
buildscript { repositories { google() } dependencies { val nav_version = "2.9.3" classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version") } }
You must also apply one of two available plugins.
To generate Java language code suitable for Java or mixed Java and Kotlin modules, add
this line to your app or module's build.gradle
file:
Groovy
plugins { id 'androidx.navigation.safeargs' }
Kotlin
plugins { id("androidx.navigation.safeargs") }
Alternatively, to generate Kotlin code suitable for Kotlin-only modules add:
Groovy
plugins { id 'androidx.navigation.safeargs.kotlin' }
Kotlin
plugins { id("androidx.navigation.safeargs.kotlin") }
You must have android.useAndroidX=true
in your
gradle.properties
file as per
Migrating to AndroidX.
生成的代码
启用 Safe Args 后,生成的代码会包含已定义的每个操作的类和方法,以及与每个发送目的地和接收目的地相对应的类。
Safe Args 为生成操作的每个目的地生成一个类。生成的类名称会在源目的地类名称的基础上添加“Directions”。例如,如果源目的地的名称为 SpecifyAmountFragment
,生成的类的名称为 SpecifyAmountFragmentDirections
。
生成的类为源目的地中定义的每个操作提供了一个静态方法。该方法接受定义的任何操作参数作为参数,并返回可直接传递到 navigate()
的 NavDirections
对象。
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 插件可在目的地之间导航。该插件可以生成简单的对象和构建器类,这些类支持在目的地之间进行类型安全的导航和参数传递。
To add Safe Args
to your project, include the following classpath
in your top level build.gradle
file:
Groovy
buildscript { repositories { google() } dependencies { def nav_version = "2.9.3" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } }
Kotlin
buildscript { repositories { google() } dependencies { val nav_version = "2.9.3" classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version") } }
You must also apply one of two available plugins.
To generate Java language code suitable for Java or mixed Java and Kotlin modules, add
this line to your app or module's build.gradle
file:
Groovy
plugins { id 'androidx.navigation.safeargs' }
Kotlin
plugins { id("androidx.navigation.safeargs") }
Alternatively, to generate Kotlin code suitable for Kotlin-only modules add:
Groovy
plugins { id 'androidx.navigation.safeargs.kotlin' }
Kotlin
plugins { id("androidx.navigation.safeargs.kotlin") }
You must have android.useAndroidX=true
in your
gradle.properties
file as per
Migrating to AndroidX.
启用 Safe Args 后,该插件会生成代码,其中包含您定义的每个操作的类和方法。对于每个操作,Safe Args 还会为每个源目的地(即生成相应操作的目的地)生成一个类。生成的类的名称由源目的地类的名称和“Directions”一词组成。例如,如果目的地的名称为 SpecifyAmountFragment
,生成的类的名称将为 SpecifyAmountFragmentDirections
。生成的类为源目的地中定义的每个操作提供了一个静态方法。该方法会将任何定义的操作形参作为实参,并返回可传递到 navigate()
的 NavDirections
对象。
例如,假设我们的导航图包含一个操作,该操作将源目的地 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 传递类型安全的数据。