Android 后退按钮和进度对话框
我有一个在工作时显示进度对话框的AsyncTask(它从doInBackground中调用runOnUiThread来显示进度对话框)。
在运行时我想允许使用后退按钮取消操作;其他人遇到了这个问题:后退按钮不起作用,而ProgressDialog is running
出于某种原因我无法回复该线程,因此必须启动另一个线程?! (改天的另一个问题)
我和 Sandy 有同样的想法,但是当 progressDialog 显示时,这个代码永远不会被调用,这是为什么?我已在我的主活动类中实现了它,progressDialog 是否会暂时将前景焦点从我的类中移开?
I have an AsyncTask that shows a progressDialog whilst working (it calls runOnUiThread from within doInBackground to show the progress dialog).
Whilst its running I want to allow the use of the back button to cancel the operation; someone else has had this problem: BACK Button is not working ,while progressDialog is running
For what ever reason I can't reply to that thread, hence having to start another?! (Another question for another day)
I had the same idea as Sandy but this code is never called whilst the progressDialog is showing, why is this? I have implemented it inside my main activity class, does the progressDialog take the foreground focus away from my class temporarily?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
首先,您应该在
OnPreExecute
中显示对话框,在OnPostExecute
中隐藏它,并在必要时通过发布进度来修改它。 (请参阅此处)现在回答您的问题:
ProgressDialog。 show()
可以采用OnCancelListener
作为参数。您应该提供一个在进度对话框实例上调用cancel()
的方法。示例:
其中
_progressDialog
是YourTask
的ProgressDialog
成员。First, you should show your dialog from
OnPreExecute
, hide it inOnPostExecute
, and - if necessary - modify it by publishing progress. (see here)Now to your question:
ProgressDialog.show()
can take aOnCancelListener
as an argument. You should provide one that callscancel()
on the progress dialog instance.example:
where
_progressDialog
is aProgressDialog
member ofYourTask
.这可以通过以下代码片段来实现:
progress 是 ProgressDialog 对象...
这将使后退按钮能够关闭对话框,但阻止任何触摸输入来执行此操作...
This can be achieved by the following code fragment:
progress is the ProgressDialog object...
This will enable the back button to close the dialog but prevent any touch input to do that...
嗯,我也有同样的问题。对我有用的最简单的方法是使用
progressDialog.setCancelable(true)
.. 这声明对话框是否可以通过按后退键取消。. 尝试一下,让我知道它是否适合您或不是。祝你好运Well, I had the same issue. The simplest method that worked for me is using
progressDialog.setCancelable(true)
.. This declares whether the dialog is cancelable by hitting the back key.. Try it and let me know if it works for you or not. Good luck我刚刚找到了解决这个问题的完美且最简单的方法。
ProgressDialog
中有一个方法可以设置KeyListener。它与
setCancelable(false)
一起工作正常。我还在检查event.isCanceled()
,因为如果没有它,我就会收到两个事件。我已经在带有和不带有硬件密钥的 Lollipop 设备上对此进行了测试。I just found a perfect and simplest solution to this problem. There's a method in
ProgressDialog
to set KeyListener.It's working fine with
setCancelable(false)
. I am also checking forevent.isCanceled()
, because without that I was getting two events. I've tested this on Lollipop device with and without Hardware keys.将后退按钮视为取消按钮并不是正确的方法。
当用户触摸对话框之外的屏幕时也会发生取消。
您想区分这两个动作,不是吗?
正确的方法是扩展 ProgressDialog 类并重写 onBackPressed 方法。
注意
setCancelable(false),再次强调后退按钮与简单的取消按钮不同。
此外,这将有效地忽略来自用户的任何其他触摸输入。
Treating back button like a cancellation is not the correct way.
Cancellation also occurs when a user touches the screen outside of the dialog box.
You want to differentiate those two actions, no?
Correct approach would be to extend the ProgressDialog class and override the onBackPressed method.
}
Notice the setCancelable(false), again emphasizing that back button is different than a simple cancellation.
Also this will effecitvely ignore any other touch inputs from the user.
这是我的解决方案。仅当应用程序退出后退按钮时才会出现 PD 问题(通常当用户重复按下它时,并且仅有时(不是所有时间)当应用程序已销毁时才会调用 PD。关闭 PD
onDestroy
由于某种原因不起作用,当我尝试它时,尽管canGoBack()
设置为 true,但每次按“后退”按钮都会关闭应用程序,而是在goBack 上关闭 PD。
这是首先导致碰撞的事件,我在finish()
之前执行此操作,如果应用程序在 goBack 上正常退出,那么首先就没有问题。 ,“差异”的目的是让用户通过快速双击(两次单击之间 400 MS)退出应用程序,而不是从一个页面返回到另一个页面
希望它可以帮助某人......
Here's my solution to this. The PD problem occurs only when the app exiting on back button (Usually when the user presses it repetitively, and only sometimes (not all the times) the PD is called when the app already destroyed. Dismissing the PD
onDestroy
doesn't work for some reason and when I've tried it, each Back button press would close the app althoughcanGoBack()
was set to true. Instead I dismiss the PD ongoBack
which is the event that causes the collision in the first place, and I do it right beforefinish()
. If the app exits on goBack normally there's no problem in the first place.BTW, 'difference' is aimed to let the user to exit the app with a fast double click (400 MS between two clicks) instead of going back from page to page.
Hope it helps someone...
javano...我使用标准技术、asyncTask 和 ProgressDialog 测试了您的场景。在我的测试中,当显示进度对话框并且我回击时,进度对话框将被关闭并且后台线程继续运行。我不知道为什么你需要调用runOnUiThread。
SO:
在onPreExecute中显示progressDialog
将长时间运行的任务放在 doInBackground 中
关闭 onPostExecute 中的进度对话框
将输入参数传递给长时间运行的任务,如下所示:
中的 asynchTask 和 ProgressDialog
cancel() onPause JAL
我有一些代码 此处。
javano... I tested your scenario using standard techniques, asyncTask and ProgressDialog. In my test, when the progressDialog is showing and i hit back, the progressdialog is dismissed and the background thread continues to run. I do not know why you need to call runOnUiThread.
SO:
show the progressDialog in onPreExecute
place the long running task in doInBackground
dismiss the progressDialog in onPostExecute
pass the input parameters to your long running task as in:
cancel() the asynchTask and progressDialog in onPause
JAL
I have some code here.
它非常简单,只需复制以下代码并粘贴到异步任务中即可。ProgressDialog
对话框;
Its very simple just copy the below code and paste within Async task..
ProgressDialog dialog;
请按照此操作,它显示取消按钮,只有异步和完成才会通过单击取消按钮来调用
Please follow this, it shows the cancel button only async and finish will call by clicking on cancel button