Reputation: 594
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.
Upvotes: 5
Views: 14563
Reputation: 389
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);
Upvotes: 2
Reputation: 4400
Just Go On to Give proper Context to this Line ::
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityName.this);
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.
Upvotes: 2
Reputation: 54322
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.
Upvotes: 1
Reputation: 180897
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.
Upvotes: 1
Reputation: 29199
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.
Upvotes: 2