Safe Args

建議您使用 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.1"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}

Kotlin

buildscript {
    repositories {
        google()
    }
    dependencies {
        val nav_version = "2.9.1"
        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 範例

舉例來說,假設有一個導覽圖包含可連接兩個目的地 (SpecifyAmountFragmentConfirmationFragment) 的單一操作,其中 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.1"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}

Kotlin

buildscript {
    repositories {
        google()
    }
    dependencies {
        val nav_version = "2.9.1"
        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 產生含有單一方法 actionSpecifyAmountFragmentToConfirmationFragment()SpecifyAmountFragmentDirections 類別,而此類別會傳回 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 傳遞類型安全的資料」。