5

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.

senps
  • 584
  • 2
  • 10
  • 30

5 Answers5

2

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.

jeet
  • 29,001
  • 6
  • 52
  • 53
  • Thanks it worked. But I want to know in which kind of a situation we have to use 'super.onBackPressed();' event – senps Jul 30 '12 at 04:32
  • 1
    super.onBackPressed must be invoked when, you want to do some operation prior to stopping the activity, and then invoke super.onBackPressed() to call default operation onBackPressed. – jeet Jul 30 '12 at 04:35
2
  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.

Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
2

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);
RedLEON
  • 281
  • 2
  • 7
  • 19
1

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.

Community
  • 1
  • 1
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
1

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.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240