调用 Continuation.resumeX() 失败一定是个问题吗?

Mar*_*ark 3 android-dialog kotlin kotlin-coroutines

suspendCoroutine用来避免在Dialogs 中使用回调。但是,在 Android 中DialogContinuation.resume()当对话框关闭时(通过在对话框区域外单击),没有明显的调用位置。如果您尝试调用,Dialog.setOnDismissListener()则必须跟踪是否已经在按钮侦听器中调用了 resume。

suspend fun displayDialog() = suspendCoroutine<String?> { continuation ->
    val builder = AlertDialog.Builder(context)
    builder.setCancelable(true)
    builder.setNegativeButton(android.R.string.cancel) { _, _ ->
        continuation.resume(null)
    }
    builder.setPositiveButton(android.R.string.ok) { _, _ ->
        continuation.resume("it's ok")
    }
    val dialog = builder.show()
    dialog.setOnDismissListener {
        // if the user clicked on OK, then resume has already been called
        // and we get an IllegalStateException
        continuation.resume(null)
    }
}
Run Code Online (Sandbox Code Playgroud)

那么,最好跟踪是否已经调用了 resume(以避免第二次调用它),或者只是不打扰resume(null)调用(在 onDismissListener 中)?

Rom*_*rov 7

Continuation 是一个低级原语,应该只恢复一次,所以你必须resume在使用它时跟踪是否已经被调用。或者,您可以使用更高级别的通信原语,例如CompletableDeferred,具有多用途complete功能:

suspend fun displayDialog(): String? {
    val deferred = CompletableDeferred<String?>()
    val builder = AlertDialog.Builder(context)
    builder.setCancelable(true)
    builder.setNegativeButton(android.R.string.cancel) { _, _ ->
        deferred.complete(null)
    }
    builder.setPositiveButton(android.R.string.ok) { _, _ ->
        deferred.complete("it's ok")
    }
    val dialog = builder.show()
    dialog.setOnDismissListener {
        deferred.complete(null)
    }
    return deferred.await()
}
Run Code Online (Sandbox Code Playgroud)