ANDROID
StartAcitivyForResult / OnActivityForResult deprecated -> registerForActivityResult
혀내
2021. 12. 4. 22:17
반응형
Description
갤러리에서 이미지를 가져오는 기능을 만들면서
StartActivityForResult가 위와 같이 deprecated되었다는 오류가 떴다.
OnActivityForResult도 마찬가지 ..😢
StartActivityForResult를 대체하는 Launcher와 registerForActivityForResult
//Open the album
var photoPickerIntent = Intent(Intent.ACTION_PICK)
photoPickerIntent.type = "image/*"
launcher.launch(photoPickerIntent);
StartActivityForResult 부분은 launch 메소드로 대체한다.
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result ->
if (result.resultCode == Activity.RESULT_OK) {
//This is path to the selected image
photoUri = result.data?.data
addphoto_image.setImageURI(photoUri)
} else {
// Exit the addPhotoActivity if you leave the album without selecting it
finish()
}
}
OnActivityForResult는 위의 코드로 바꿔주면 끝
참고자료 : https://developer.android.com/training/basics/intents/result?hl=ko
반응형