Sitemap

ActivityResultLauncher

Feb 23, 2021

--

onActivityResult 가 deprecated 되었습니다….

하지만! ActivityResultLauncher 가 나왔습니다.
기존보다 더 간편하고 원하는 것이 나왔습니다.

바로 callback!

먼저 선 작업을 해줘야하는 부분이 있습니다.

def activity_version = "1.2.0"
implementation "androidx.activity:activity-ktx:$activity_version"
def fragment_version = "1.3.0"
implementation "androidx.fragment:fragment-ktx:$fragment_version"

app/build.gradle 에 위와 같이 dependencies 추가를 해줍니다.

Get parkwow13’s stories in your inbox

Join Medium for free to get updates from this writer.

그리고 사용방법은 아래와 같습니다.

현재 위와 같이 지원하는 목록입니다.

https://developer.android.com/reference/androidx/activity/result/contract/package-summary

Tip. 위의 링크에서 보면 ActivityResultContracts.StartActivityForResult
이렇게 표시만 되어있고 ActivityResultLauncher 의 generic 에는 어떤 타입이 들어가야하는지 모를 수가 있습니다.

그럼 위 링크의 해당 클래스를 클릭해보면 아래와 같이 상속받고있음을 확인할 수 있습니다

ActivityResultContract<Intent, ActivityResult>

앞에 부분이 ActivityResultLauncher<Intent> 와 같이 제네릭에 들어갈 타입이고 뒤에 부분이 response 로 받는 타입입니다.

--

--