Android后退按钮和进度对话框 [英] Android Back Button and Progress Dialog
问题描述
我有一个AsyncTask ,它在工作时显示一个progressDialog(它从 doInBackground 中调用 runOnUiThread 来显示进度对话框) p>
虽然它的运行我想允许使用后退按钮取消操作;有其他人遇到此问题: BACK按钮不工作,而progressDialog正在运行
有什么原因我无法回复该线程,因此不得不启动另一个? (另一天的另一个问题)
我和Sandy有同样的想法,但是在 progressDialog 显示时,这个代码从来没有被调用,为什么这个?我已经在我的主要活动类中实现了它,而进阶对话框是否将临时焦点从我的课程中脱离出来?
首先,您应该从 OnPreExecute
中显示您的对话框,将其隐藏在 OnPostExecute
中,如果需要 - 通过发布进度来修改它。 (请参阅此处)
现在对于您的问题: ProgressDialog.show()
可以采用 OnCancelListener
作为参数。您应该在进度对话框实例上提供一个调用 cancel()
。
示例:
@Override
protected void onPreExecute(){
_progressDialog = ProgressDialog.show(
YourActivity.this,
标题,
消息,
true,
true,
new DialogInterface.OnCancelListener(){
@Override
public void onCancel(DialogInterface对话框){
YourTask.this.cancel(true);
finish();
}
}
);
}
其中 _progressDialog
ProgressDialog
成员 YourTask
。
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?
First, you should show your dialog from OnPreExecute
, hide it in OnPostExecute
, and - if necessary - modify it by publishing progress. (see here)
Now to your question: ProgressDialog.show()
can take a OnCancelListener
as an argument. You should provide one that calls cancel()
on the progress dialog instance.
example:
@Override
protected void onPreExecute(){
_progressDialog = ProgressDialog.show(
YourActivity.this,
"Title",
"Message",
true,
true,
new DialogInterface.OnCancelListener(){
@Override
public void onCancel(DialogInterface dialog) {
YourTask.this.cancel(true);
finish();
}
}
);
}
where _progressDialog
is a ProgressDialog
member of YourTask
.
这篇关于Android后退按钮和进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!