Android Activity Result APIs
사용 버전: Android Studio 4.0.1
사용 언어: Kotlin 1.4.10
사용 언어: Kotlin 1.4.10
안드로이드 Activity Result APIs를 알아보겠습니다.

androidx의 fragment 라이브러리에서 1.3.0-alpha04부터
startActivityForResult()와 onActivityResult(), requestPermissions(),
onRequestPermissionsResult()가 fragment에서 deprecated 되었습니다.

위의 함수들 대신에 사용할 수 있는 것이 Activity Result APIs입니다.
물론 지금은 Alpha 버전이라서 신경 쓸 필요는 없습니다. 일부러 Alpha 버전으로
업데이트를 하지 않는 이상 말이죠.
하지만, 언젠가는 정식 버전으로 Release 될 것이고 사용하게 될 것입니다.
우리는 미리 한 번 사용해보겠습니다. Permission 요청을 받는 걸로
해보겠습니다.
앱 용 build.gradle에 아래를 추가합니다.

Sync Now를 누릅니다.
AndroidManifest.xml에 CAMERA 권한을 입력합니다.

이제 Fragment로 갑니다.
onCreateView 위에 변수를 정의합니다.

지금은 ActivityResultContracts.RequestPermission을 사용했는데요. 다른 것들도
사용 가능합니다.

그리고 onCreateView에 button을 클릭하면 작동되도록 setOnclickListenr를
만듭니다.

실행해 봅시다.

거부를 누른 모습.

허용을 누른 모습.

끝.
댓글
댓글 쓰기
궁금한 점은 댓글 달아주세요.
Comment if you have any questions.