Last active
June 4, 2021 08:47
-
-
Save kibotu/d9114ba953d0bdf87490f88ddc91263b to your computer and use it in GitHub Desktop.
Lifecycle.waitForLifecycleResumeEvent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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