is it a good idea to wrap Activity Result api into...
# android
d
is it a good idea to wrap Activity Result api into coroutines? so instead of
Copy code
val getContent = registerForActivityResult(GetContent()) { uri: Uri? ->
    // Handle the returned Uri
}
getContent.launch("image/*")
we could use something like
Copy code
val getContent = registerForActivityResult(GetContent(), suspendableContract)
val result = getContent.launchSuspendable("image/*")
f
You can just wrap the callback like any other with
suspendCancellableCoroutine
d
sure. but is it a good idea? i mean what if process will be killed during app in background?
f
not really different than with the lambda I would assume, since it is not any lifecycle related callback but have not tested it yet.
o
It won't really work especially when the process is killed before you return to the app.
d
it won't work in both cases? i mean if i use classic onActivityResult? does it trigger after process recreation?
ok, i found that both (old onActivityResult api and newer Api Result api) survive the process killing. so it would be a tricky to wrap this into a coroutine. but looks like possible though. i found this POC lib https://github.com/pdvrieze/android-coroutines it uses coroutine serialization.
@pdvrieze sorry to bother you, does your lib survive the process kill e.g. in startForResult use case?
i
Yeah, that's the very reason the API isn't just a regular callback. Any lambdas, scopes, methods, variables, they're all no longer there when the process is killed because of low memory.
👍 2
You could certainly have the callback you provide to the Activity Result API write that result to a flow you collect, etc.