Pick the fix
This crashes with 'LifecycleOwner is attempting to register while current state is RESUMED'. What's the fix?
class AvatarActivity : ComponentActivity() {
fun onPickClicked() {
val picker = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
render(uri)
}
picker.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
}
}Hint 1 of 1
When must registerForActivityResult be called?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Register the launcher as a property, and only call launch() in the click handler
Correct. Launchers must be registered before the owner is started.
Why not the others?
- B Call it inside onResume. Still too late.
- C Use startActivityForResult. Deprecated, and doesn't fix the design.
- D Wrap it in runOnUiThread. It's already on the main thread; timing is the problem.