Question

I have an activity which has a progressdialog box with setCancelable(false).

I want to finish the activity as well as dismiss the progressdialog box on backPressed().

I have implemented onBackPressed() as below but it is not working.

@Override
public void onBackPressed() {
    if(pDialog.isShowing()){
        pDialog.dismiss();
    }
    this.finish();
    super.onBackPressed();
}

progress Dialog code is

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setTitle("Connecting to Server");
        pDialog.setMessage("Updating Assignee Detail. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
Was it helpful?

Solution

Set

pDialog.setCancelable(true);

And rest of your code is fine.

OTHER TIPS

Just dismiss the dialog in OnStop() rest Activity will finish() automatically by pressing back

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    if(progressDialog.isShowing()) {
        progressDialog.dismiss();
    }
}

Happy Coding!!

You can achieve it in 2 following steps,

Step-1 Change

pDialog.setCancelable(false) to

pDialog.setCancelable(true)

because a Dialog can't be cancelled if it is set to false.

Step-2

Add a listener to your Dialog so that it will listen when cancelled like following,

pDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                // if from activity
                finish();

            }

        });

Use this method and no need to call the finish for the activity:

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

intead finish activity on dialog dismiss:

pDialog .setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                finish();
            }
        });

This is a little example of progress dialog, close it and activity...

Option 1

Create progress dialog

ProgressDialog dialog = new ProgressDialog(this); //create progress dialog
    dialog.setMessage("¡wait it, please...!"); //body text...
    dialog.setCancelable(true); //if it will be cancelable
    //this is the listener cancelable
    dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            finish();
        }
    });
    dialog.show(); //show it...

finish it in some threat or wherever you want...

 dialog.dismiss();

on backpressed

@Override
public void onBackPressed() {
    if(dialog.isShowing()){
        dialog.cancel();
    }
    finish();
}

Option 2

Create a global var

Boolean validation=false;

Create progress dialog

ProgressDialog dialog = new ProgressDialog(this); //create progress dialog
    dialog.setMessage("¡wait it, please...!"); //body text...
    dialog.setCancelable(false); //if it will be cancelable
    //this is the listener dismiss
    dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                if(validation){
                    dialogInterface.dismiss();
                }else{
                    finish();
                }
            }
        });
    dialog.show(); //show it...

finish it

 //if finish process 
 validation=true;
 dialog.dismiss();

or

 //if finish with backpress
 validation=false;
 dialog.dismiss();

on backpressed

@Override
public void onBackPressed() {
    if(dialog.isShowing()){
        validation=false;
        dialog.dismiss();
    }
    finish();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top