Android Jetpack

Jetpack Navigation - 5 (Safe Args)

개복치 개발자 2022. 2. 19. 05:50

개복치개발자 강의는 아래의 링크에서 확인할 수 있습니다.

 

개복치개발자 | Linktree

uyalae@naver.com

linktr.ee

 

 Navigation에서 Fragment끼리 데이터를 전달하는 방법에 대해 Bundle에 대해서 알아봤습니다.

 

 그런데 공식문서에서는 Safe Args를 이용하는 것에 대해서 나와있습니다.

 

 왜 기존 Bundle을 계속 이용하면 되지 Safe Args를 사용하는 것일까요?

 

 이에 대해 잘 설명해놓은 글이 있습니다.

 

https://www.raywenderlich.com/19327407-using-safe-args-with-the-android-navigation-component

 

Using Safe Args With the Android Navigation Component

In this Safe Args tutorial, you’ll learn how to safely pass data between different destinations while using the Android Navigation Component.

www.raywenderlich.com

 

Why Safe Args?

Before the introduction of Safe Args, passing data when navigating to a different screen required Bundles. The first screen, the sender, would build a Bundle instance and populate it with data. The second screen, the receiver, would later retrieve this data. This manual approach of sending and unwrapping data is unreliable, as the sender and receiver must agree on:

 

왜 Safe Args인가?

Safe Args가 도입되기 전에는 다른 화면으로 이동할 때 데이터를 전달하기 위해 Bundle이 필요했습니다. 첫 번째 화면인 발신자는 번들 인스턴스를 빌드하고 데이터로 채웁니다. 두 번째 화면인 수신기는 나중에 이 데이터를 검색합니다. 송신자와 수신자가 다음 사항에 동의해야 하므로 데이터를 보내고 랩핑 해제하는 이 수동 접근 방식은 신뢰할 수 없습니다.

 

 데이터 안정성을 위해 필요하다고 적혀있습니다.

 

 그러면 이 친구를 어떻게 사용하는지에 대해 알아보겠습니다.

 

 Project Gradle에 아래와 같이 입력해줍니다.

 

buildscript {
    dependencies {
        def nav_version = "2.3.5"
        classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
    }
}

 

 

 Module Gradle에 아래와 같이 입력해주고

 

implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'

 

 Graph에서 Argument 넣어주고

 

 

 

 나머지 Fragment에서 아래와 같이 처리해줘서 데이터 넘겨줍니다.

 

 

 

 

- 참조

 

https://developer.android.com/guide/navigation/navigation-pass-data?hl=ko 

 

대상 간 데이터 전달  |  Android 개발자  |  Android Developers

대상 간 데이터 전달 탐색을 사용하면 대상 인수를 정의하여 탐색 작업에 데이터를 첨부할 수 있습니다. 예를 들어 사용자 프로필 대상은 사용자 ID 인수를 취해 표시할 사용자를 결정할 수 있습

developer.android.com

 

https://www.youtube.com/watch?v=vx1-V3HH0IU 

https://www.raywenderlich.com/19327407-using-safe-args-with-the-android-navigation-component

 

Using Safe Args With the Android Navigation Component

In this Safe Args tutorial, you’ll learn how to safely pass data between different destinations while using the Android Navigation Component.

www.raywenderlich.com