简体   繁体   中英

Android-How to close progress dialog when back button is pressed

按下后退按钮时如何关闭进度对话框?

A much better way.

          ProgressDialog dialog = new ProgressDialog(this);
          dialog.setCancelable(true);
          dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
             @Override
             public void onCancel(DialogInterface dialog){
                /****cleanup code****/
          }});           

The setCancelable method tells the ProgressDialog to close when the back button is pressed. The listener at the end allows you to do anything that may need to be done as a result of a cancel (like closing a socket).

Well i found my approach much more useful. if you set progressDialog.setCancelable(true); it will cancel the dialog if the user would press anywhere on the screen outside the dialog. You wouldn't want that, right? If you want the progress dialog to cancel only if the user presses the back button then use this code:

ProgressDialog pDialog;

pDialog = new ProgressDialog(MainActivity.this) {
             @Override
                public void onBackPressed() {
                    pDialog.dismiss();
                }};
        pDialog.setMessage("Loading");
         pDialog.setCancelable(false);

         pDialog.show(); 

Here is the solution.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
       dialog.dismiss();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

or also you can dismiss dialog in onPause method of activity.

I would honestly just do:

The Dialog interface provides a dialog.setCanceledOnTouchOutside(false) method which enables precisely this. You call that with a false value and the user won't be able to press the back button to go back to your activity

    ProgressDialog progressDialog = ProgressDialog.show(ActivityName.this, 
                                    "Title","Message");
    progressDialog.setCancelable(true);
    progressDialog.setOnCancelListener(new OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            // TODO Auto-generated method stub
            // Do something...
        }
    });

When you press the back button, onCancel is called.

By default progress dialog get dismiss , if you click on back button . Or, you can add this line:

progress.setCancelable(true);

Another option is you can call finish() and then progress.dismiss() :

progress.setOnKeyListener(new ProgressDialog.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface arg0, int keyCode,
                KeyEvent event) {
            // TODO Auto-generated method stub
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                finish();
                dialog.dismiss();
            }
            return true;
        }
});

Or, you override method onCancel() method in back key pressed button event .

ProgressDialog dialog = new ProgressDialog(yourActivity.this);
dialog.setCancelable(true);
.
.
.
progressDialog.show();

Hope, this will work.

It's very simple just copy the below code and paste within Async..

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....");
    dialog.setCancelable(false);
    dialog.show();
}

Here is one possible solution:

Dialog dialog;
dialog = new Dialog(context,style);
dialog.setCanceledOnTouchOutside(false);

You can dismiss dialog in onPause method of activity. Here is the solution. It worked in case of me.

@Override
    public void onPause() {
        super.onPause();
        progressDialog.dismiss();
    }

Make sure private ProgressDialog progressDialog; declared globally

and initialize progressDialog on onCreate method just like this:

progressDialog = new ProgressDialog(YourActivityGoesHere.this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM