简体   繁体   中英

Yes/NO Alert Dialog box in Android

I want to Show an Alert Dialog Box in android on onBackPressed() event

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

    //@Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
        case DialogInterface.BUTTON_POSITIVE:
            //Yes button clicked
            break;

        case DialogInterface.BUTTON_NEGATIVE:
            //No button clicked
            break;
        }
    }
};

But I am getting error when executing it in onBackPressed() event

@Override
public void onBackPressed() {
    super.onBackPressed();  
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
        .setNegativeButton("No", dialogClickListener).show();

}

Error : " com.java.mypkg has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@406c3798 that was originally added here "

Do I have missed something. Pls help.

Yes, don't invoke that as per previous user's response. super.onBackPressed(); will onStop method of the Activity. Instead of onBackPressed(); you can use onKeyDown for your requirement. If you need to open an AlertDialog when you press the back button you can simply try that with KeyEvent

For example -

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub

    switch(keyCode)
    {
    case KeyEvent.KEYCODE_BACK:
        AlertDialog.Builder ab = new AlertDialog.Builder(AlertDialogExampleActivity.this);
        ab.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
        .setNegativeButton("No", dialogClickListener).show();
        break;
    }

    return super.onKeyDown(keyCode, event);
}

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
        case DialogInterface.BUTTON_POSITIVE:
            //Yes button clicked
            break;

        case DialogInterface.BUTTON_NEGATIVE:
            //No button clicked
            break;
        }
    }
};

When you're overriding onKeyDown method it will detect back key with your KEYCODE_BACK

Hope this helps you.

Dont invoke super.onBackPressed(); cause it will call onStop method of the activity.

And Displaying a dialog over an activity which has been finished, will leak window.

  1. Just Go On to Give proper Context to this Line ::

     AlertDialog.Builder builder = new AlertDialog.Builder(ActivityName.this); 
  2. Close the Alert Dialog proplerly like this.

     new AlertDialog.Builder(ActivityName.this) .setMessage("You have to Login first to apply.\\nWant to login ?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Perform Your Task Here--When Yes Is Pressed. dialog.cancel(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Perform Your Task Here--When No is pressed dialog.cancel(); } }).show(); 

This Error of Window leaked is caused when ::

  • A new Window with different context is opened in the Activity having different Context.

  • A Window Or Dialog is not properly closed while exiting the Activity.

My alertdialog method:

public void message_dialog_yes_no (Activity activity, String msg, DialogInterface.OnClickListener yesListener) {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);

    builder.setMessage(msg)
           .setCancelable(false)
           .setPositiveButton("Yes", yesListener) 
           .setNegativeButton("No",  new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
              }})              
           .show();
}

After declared this method I can call it this way:

DialogInterface.OnClickListener yesListener;

        yesListener = new DialogInterface.OnClickListener() {               
            public void onClick(DialogInterface dialog, int which) {
//codes
        }
        };

message_dialog_yes_no(this, "Confirm delete?" , yesListener);

You should only call super.onBackPressed() if you want to do the default action (ie actually go back) and not if you want to stay in the Activity.

See this link for an example how to override onBackPressed() correctly.

We usually override onBackPressed() to execute some conditions when we exit the Activity. It means that we are actually by passing the normal execution of the Back Press Event which is nothing but super.onBackPressed . So having it inside the overrided method means that it will also follow the default executions that will be performed when the back key is pressed and also our own methods will also be executed.

But in your case since you are trying to show a AlertDialog after invoking the super class, your Activity context is no more available which means there is no window for your Alert Dialog to show itself, and hence the leaked window error.

In this case, you have remove the super class invocation. Simple.

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