Android的后退按钮和进度对话框 [英] Android Back Button and Progress Dialog

查看:157
本文介绍了Android的后退按钮和进度对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AsyncTask的,显示了progressDialog同时工作(它调用runOnUiThread从内部doInBackground显示进度对话框)。

I have an AsyncTask that shows a progressDialog whilst working (it calls runOnUiThread from within doInBackground to show the progress dialog).

虽然它的运行我想允许使用后退按钮的取消操作;其他人遇到过这个问题:<一href="http://stackoverflow.com/questions/4919676/back-button-is-not-working-while-progressdialog-is-running">BACK按钮不工作,而progressDialog运行。

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)

我有同样的想法桑迪但这code不会被调用,而该progressDialog正显示出,这是为什么?我已经实现了它在我的主要活动类,请问progressDialog采取前景注意力从我的课暂时?

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?

推荐答案

首先,你应该表现出从的对话框preExecute ,把它藏在 OnPostExecute ,和 ​​- 必要时 - 通过出版进度进行修改。 (见这里

First, you should show your dialog from OnPreExecute, hide it in OnPostExecute, and - if necessary - modify it by publishing progress. (see here)

现在你的问题: ProgressDialog.show()可以采取 OnCancelListener 作为参数。您应该提供一个叫取消()上的进度对话框实例。

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.

例如:

    @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();
                    }
                }
        );
    }

其中, _progressDialog YourTask ProgressDialog 成员

这篇关于Android的后退按钮和进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆