1

I have a dialog showing up using this code:

 Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("New game")
                        .setCancelable(false)
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        clear();
                                    }
                                }).show();

but when I rotate my screen i get an error : android.view.WindowLeaked and i dont know why. I tried writing dialog.dismiss() and dialog.cancel() in the onClick function of the dialog but it didnt work.

Thank you!

small
  • 223
  • 1
  • 2
  • 6

2 Answers2

0

Have a look at the below link of a post on StackOverflow, and read the answer about using DialogFragment and why:

Prevent dialog dismissal on screen rotation in Android

Community
  • 1
  • 1
Kevin Crain
  • 1,905
  • 1
  • 19
  • 28
0

Try this:

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
            builder1.setMessage("Write your message here.");
            builder1.setCancelable(true);
            builder1.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
            //put your code that needed to be executed when okay is clicked
            dialog.cancel();


            }
            });
            builder1.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert11 = builder1.create();
            alert11.show();
Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50
  • Thank you!, using the `AlertDialog alert11` this way i could dismiss it anywhere, which is what i needed. – small Mar 22 '15 at 12:52