Skip to content

Instantly share code, notes, and snippets.

@kibotu
Last active June 4, 2021 08:47
Show Gist options
  • Save kibotu/d9114ba953d0bdf87490f88ddc91263b to your computer and use it in GitHub Desktop.
Save kibotu/d9114ba953d0bdf87490f88ddc91263b to your computer and use it in GitHub Desktop.
Lifecycle.waitForLifecycleResumeEvent
@OptIn(ExperimentalCoroutinesApi::class)
suspend fun Lifecycle.waitForLifecycleResumeEvent() = suspendCancellableCoroutine<Unit> { continuation ->
val observer = object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume() {
removeObserver(this)
continuation.resume(Unit) {
// it.printStackTrace()
}
}
}
addObserver(observer)
continuation.invokeOnCancellation {
removeObserver(observer)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment