Skip to content

Instantly share code, notes, and snippets.

@elihart
Created November 20, 2019 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elihart/863809916a7f2dc11e828336c591c0d8 to your computer and use it in GitHub Desktop.
Save elihart/863809916a7f2dc11e828336c591c0d8 to your computer and use it in GitHub Desktop.
HowToWaitForIdleHandlers
fun waitForLoopers(loopers: List<Looper>) {
val idleDetectors = loopers.map { HandlerIdleDetector(Handler(it)) }
while (idleDetectors.any {  !it.isIdle }) {
}
}
class HandlerIdleDetector(val handler: Handler) {
var isIdle = false
init {
postIdleCheck()
}
private fun postIdleCheck() {
handler.post {
do {
isIdle = handler.looper.queue.isIdle
} while (isIdle)
postIdleCheck()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment