Paul Woitaschek
04/29/2019, 3:17 PMstartActivityForResult
and it suspends until onActivityResult
is called.
Is using a Channel the right way to do that?
kotlin
class MyActivty : Activity(){
private val channel = Channel<Uri>()
override fun onActivityResult(data: Intent){
val uri = data.data
channel.offer(uri)
}
suspend fun soSthWithStartActivtyForResult() : Uri {
startActivityForResult(intent, 42)
return channel.receive()
}
}
streetsofboston
04/29/2019, 3:18 PMsuspendCancellableCoroutine
for this.Paul Woitaschek
04/29/2019, 3:19 PMstreetsofboston
04/29/2019, 3:20 PMPaul Woitaschek
04/29/2019, 3:28 PMsuspendCoroutine
- there is nothing cancellable when calling startActivityForResult
.streetsofboston
04/29/2019, 3:29 PMsuspendCoroutine
is good enough.soSthWithStartActivtyForResult
when the launching activity is cancelled, i.e. onActivityResult is never called.class MyActivity : Activity(), CoroutineScope by MainScope() {
private var launchContinuation: CancellableContinuation<Uri>? = null
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
launchContinuation?.resume(data!!.data)
}
suspend fun soSthWithStartActivityForResult() : Uri = suspendCancellableCoroutine {
launchContinuation = it
startActivityForResult(intent, 42)
}
override fun onDestroy() {
super.onDestroy()
launchContinuation?.cancel()
}
}
nulldev
04/29/2019, 3:56 PMlouiscad
04/29/2019, 5:44 PMhannesstruss
04/30/2019, 8:57 AMlouiscad
04/30/2019, 9:09 AMdekans
04/30/2019, 9:22 AM