Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Prompting an AlertDialog onBackPressed

I am trying to finish up my main menu in my application. I thought it would be a simple nice touch to add an AlertDialog in the OnBackPressed method. However for some reason I am getting all kinds of errors.

I created the AlertDialog in the OnBackPressed and show it but the app just closes when I press the back button and I get errors saying that the window is leaking.

Any idea how to fix this? I searched for about 30 minutes and couldn't find anyone else with this problem.

like image 888
droider Avatar asked Jun 05 '12 22:06

droider


3 Answers

Try to not call super.OnBackPressed(), this code shoul help:

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    MyActivity.this.finish();
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });
    AlertDialog alert = builder.create();
    alert.show();

}
like image 165
K_Anas Avatar answered Oct 17 '22 06:10

K_Anas


If you want to call super.onBackPressed() use this code:

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //finish();
                    MyActivity.this.onSuperBackPressed();
                    //super.onBackPressed();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
    /*
    if (handleCancel()){
        super.onBackPressed();
    }
    */
}

public void onSuperBackPressed(){
    super.onBackPressed();
}

I just added a new public method onSuperBackPressed with the super-method to MyActivity.

Thanks goes to K_Anas for providing the excellent idea.

like image 28
ckarrie Avatar answered Oct 17 '22 05:10

ckarrie


I created the AlertDialog in the OnBackPressed and show it but the app just closes when I press the back button and I get errors saying that the window is leaking.

If inside your OnBackPressed you are calling super.OnBackPressed() then the application will finish, as that's what the base implementation of OnBackPressed does. Don't call the super method, and the application won't close.

like image 42
K-ballo Avatar answered Oct 17 '22 06:10

K-ballo