대상 간 이동을 위해 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.6" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } }
Kotlin
buildscript { repositories { google() } dependencies { val nav_version = "2.9.6" 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는 단일 메서드(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.6" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } }
Kotlin
buildscript { repositories { google() } dependencies { val nav_version = "2.9.6" 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는 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를 사용하여 유형 안전성을 갖춘 데이터 전달을 참고하세요.