Android Back Button and Progress Dialog

2019-03-08 14:21发布

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?

9条回答
爷的心禁止访问
2楼-- · 2019-03-08 14:33

Its very simple just copy the below code and paste within Async task..

ProgressDialog dialog;

@Override
protected void onPreExecute() {

    dialog = new ProgressDialog(MainActivity.this) {
        @Override
        public void onBackPressed() {
            dialog.cancel();
            dialog.dismiss();
        }
    };
    // dialog.setTitle("file is..");//its optional If u want set title in progress
    // bar
    dialog.setMessage("Loading file....");
    dialog.setCancelable(false);

    dialog.show();

}
查看更多
ら.Afraid
3楼-- · 2019-03-08 14:34

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.

This class was deprecated in API level 26. ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar, which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress. LINK

查看更多
爷、活的狠高调
4楼-- · 2019-03-08 14:37

I just found a perfect and simplest solution to this problem. There's a method in ProgressDialog to set KeyListener.

progressDialog.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK && !event.isCanceled()) {
            if(progressDialog.isShowing()) {
                //your logic here for back button pressed event
            } 
            return true;
        }
        return false;
    }
});

It's working fine with setCancelable(false). I am also checking for event.isCanceled(), because without that I was getting two events. I've tested this on Lollipop device with and without Hardware keys.

查看更多
家丑人穷心不美
5楼-- · 2019-03-08 14:41

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

查看更多
虎瘦雄心在
6楼-- · 2019-03-08 14:44

Please follow this, it shows the cancel button only async and finish will call by clicking on cancel button

protected void onPreExecute() {


        dialogx.setMessage("Loading... Please Wait...");
        dialogx.setCancelable(false);
        dialogx.setButton(DialogInterface.BUTTON_NEGATIVE,
                "Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialogx.dismiss();
                GetDataTask.this.cancel(true);
                finish();
            }
        });
        dialogx.show();


    }
查看更多
冷血范
7楼-- · 2019-03-08 14:46

This can be achieved by the following code fragment:

progress.setCancelable(true);
progress.setCanceledOnTouchOutside(false);

progress is the ProgressDialog object...

This will enable the back button to close the dialog but prevent any touch input to do that...

查看更多
登录 后发表回答