当 Android 对话框回答太快时,代码未执行(竞争条件)
在我的 Android 应用程序中,我有一个对话框,其中包含如下代码:
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
doStuff();
doMorestuffThatTakesTime();
}
})
我注意到的是,如果我非常快地按下“是”按钮,则 doStuff() 中的代码不会被执行。我想知道dialog.cancel()命令是否与它有关,但即使它移到最后并完全删除,问题仍然存在。
感觉像是某种竞争条件,但问题是什么以及我应该如何解决这个问题?
In my android app, I have a dialog box which has code like this:
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
doStuff();
doMorestuffThatTakesTime();
}
})
The thing I noticed is, that if I press the 'Yes' button very quickly, code in the doStuff() doesn't get executed. I wondered if the dialog.cancel() order had anything to do with it, but the problem exists even with it moved to the end and also removed altogether.
It feels like some sort of race condition, but what is the problem and how should I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
警告。更多概念验证代码:
您可以尝试在单击时启动一个线程,如下所示:
并将消息捕获在单个活动处理程序中:
如果您尝试从新线程触摸 UI,这将不起作用。
Warning. Yet more proof of concept code:
You could try launching a thread in on click as in:
And trap the message in the single Activity Handler:
This will NOT work if you try to touch the UI from the new thread.