デスティネーション間を移動する際は、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.4" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } }
Kotlin
buildscript { repositories { google() } dependencies { val nav_version = "2.9.4" 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
」という名前のクラスが生成されます。
生成されるクラスには、送信側デスティネーション内で定義されている各アクション用の静的メソッドが格納されます。このメソッドは、定義済みのアクション パラメータを引数として受け取り、NavDirections
オブジェクトを返します。このオブジェクトは直接 navigate()
に渡すことができます。
Safe Args の例
たとえば、SpecifyAmountFragment
と ConfirmationFragment
の 2 つのデスティネーションを接続する単一のアクションを持つナビゲーション グラフについて考えてみましょう。ConfirmationFragment
は、アクションの一部として指定する単一の float
パラメータを取ります。
Safe Args は、単一のメソッド actionSpecifyAmountFragmentToConfirmationFragment()
を持つ SpecifyAmountFragmentDirections
クラスと、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.4" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } }
Kotlin
buildscript { repositories { google() } dependencies { val nav_version = "2.9.4" 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
」という名前のクラスが作成されます。生成されるクラスには、送信側デスティネーション内で定義されている各アクション用の静的メソッドが格納されます。このメソッドは、定義済みのアクション パラメータを引数として受け取り、NavDirections
オブジェクトを返します。このオブジェクトは navigate()
に渡すことができます。
たとえば、送信側デスティネーション「SpecifyAmountFragment
」を受信側デスティネーション「ConfirmationFragment
」に接続する単一のアクションを持つナビゲーション グラフがあるとします。
Safe Args は、NavDirections
オブジェクトを返す単一メソッド「actionSpecifyAmountFragmentToConfirmationFragment()
」を持つ SpecifyAmountFragmentDirections
クラスを生成します。この返された 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 を使用してタイプセーフにデータを渡すをご覧ください。