Skip to content

Instantly share code, notes, and snippets.

@rabidaudio
Created September 16, 2017 20:42
Show Gist options
  • Save rabidaudio/362812e877ab874a70197d8fa739f9cc to your computer and use it in GitHub Desktop.
Save rabidaudio/362812e877ab874a70197d8fa739f9cc to your computer and use it in GitHub Desktop.
Kotlin Coroutine Android examples
suspend fun showConfirmationDialog() = suspendCancellableCoroutine<Boolean> { cont ->
val dialog = AlertDialog.Builder(this)
.setMessage("Are you sure?")
.setPositiveButton("Yes", { _, _ -> cont.resume(true) })
.setNegativeButton("No", { _, _ -> cont.resume(false) })
.setCancelable(true)
.setOnCancelListener { cont.cancel() }
.create()
dialog.show()
cont.invokeOnCompletion { dialog.dismiss() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment