Islam Hassan
Islam Hassan

Reputation: 1736

Android - How to show AlertDialog before finishing activity?

I'm trying to implement this code

    if(someCondition){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("message")
        .setPositiveButton("Yes", this)
        .setNegativeButton("No", this);
        builder.show();
    }
    finish();

The problem is that the activity calls finish() before dialog is shown up so it throws the following exception

MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40704090 that was originally added here

How to handle that?

Upvotes: 2

Views: 5103

Answers (4)

Sam
Sam

Reputation: 86948

Use an OnDismissListener to call finish() when the dialog is closed. This way the user can respond to the Dialog and the Activity can still be closed.


if(someCondition) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("message");
        .setPositiveButton("Yes", this)
        .setNegativeButton("No", this);

    AlertDialog dialog = builder.create();
    dialog.setOnDismissListener(new OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            finish();
        }
    });
    dialog.show();
}
else {
    finish();
}

Upvotes: 3

Rolf ツ
Rolf ツ

Reputation: 8781

I'm not sure what the question really is, but the error you are getting is because you finish the activity while a dialog is showing. What i often do is call dialog.dismiss() in the onPause or onStop method of my activity.

What about this code?:

Some where in your activity;

private AlertDialog dialog;

the onPause or onStop method,

@Override
public void onPause() {
    super.onPause();
    if(dialog != null){
        dialog.dismiss();
    }
}

Building the dialog,

    if(someCondition){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("message")
        .setPositiveButton("Yes", new OnClickListener(){
            public void onClick(DialogInterface arg0, int arg1) {
                //do stuff
                 //finish?
            }
        });
        .setNegativeButton("No", new OnClickListener(){
            public void onClick(DialogInterface arg0, int arg1) {
                //do stuff
                //finish?
            }
        });
        dialog = builder.show();
    }

Upvotes: 0

Luke Taylor
Luke Taylor

Reputation: 9599

You are getting the following error:

MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40704090 that was originally added here

because the builder has to be dismissed before the activity is finished. So I guess you will have to redesign how you would like your activity to be finished. Perhaps you would like it to finish when the "No" button is clicked?

I hope this helps.

Upvotes: 0

schlingel
schlingel

Reputation: 8575

if(someCondition){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("message")
    .setPositiveButton("Yes", this)
    .setNegativeButton("No", this);
    builder.show();
} else {
    finish(); 
}

And in the Handler for the button click put the finish again.

Upvotes: 3

Related Questions