首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用Continuation.resumeX()失败一定是个问题吗?

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

Stack Overflow用户
提问于 2019-03-24 19:41:52
回答 1查看 1K关注 0票数 3

我使用suspendCoroutine是为了避免在Dialogs中使用回调。然而,在Android Dialogs中,当对话框被关闭时(通过在对话框区域之外单击),没有明显的地方可以调用Continuation.resume()。如果您尝试在Dialog.setOnDismissListener()中调用,则必须跟踪按钮侦听器中是否已经调用了resume。

代码语言:javascript
复制
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)
    }
}

那么,跟踪resume是否已经被调用(以避免第二次调用它),或者只是不去关心resume(null)调用(在onDismissListener中)是更好呢?

EN

回答 1

Stack Overflow用户

发布于 2019-03-24 22:16:46

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

代码语言:javascript
复制
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()
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55323411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档