How do I show only one Dialog at a time?
Asked Answered
A

7

12

My Android application shows an AlertDialog on a button click. When I click on the button more than once more than one Dialog is created. How can I fix this?

Here is my code:

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog =  new AlertDialog.Builder(context);             
        dialog.show();
    }
});
Azotemia answered 24/9, 2012 at 7:54 Comment(2)
Instead of AlertDialog.Builder, I am using a custom class extends AlertDialog. which shows an image.No buttons.Azotemia
Guess you can still use the same concept. Just create a type of flag :)Kurtiskurtosis
K
7

You can create a global flag (boolean) that is set to true if a dialog is shown? If the user click ok, yes, no or anything the dialog is closed and you set the flag to false.

So something like:

boolean dialogShown;

If(dialogShown)
{
  return;
}
else
{
  dialogShown = true;
  dialog =  new AlertDialog.Builder(context);              
  dialog.show();
}
Kurtiskurtosis answered 24/9, 2012 at 8:7 Comment(1)
always put blocks in brackets, even one-liners. You save yourself a lot of troubles (especially when using languages with preprocessor and macros)Bramblett
S
22

you need to check if dialog isShowing or not

Dialog has an isShowing() method that should return if the dialog is currently visible.

public AlertDialog myDialog;

public void showDialog(Context context) {
    if( myDialog != null && myDialog.isShowing() ) return;

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Title");
    builder.setMessage("Message");
    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int arg1) {
                dialog.dismiss();
            }});
    builder.setCancelable(false);
    myDialog = builder.create();
    myDialog.show();
  }
Submersible answered 1/12, 2014 at 11:30 Comment(1)
Wrong example. Error scenario (Android 9): 1. Show dialogue. 2. Switch to another application. 3. Within 15 seconds, the operating system will switch the application to the background mode. 4. Return to the application - the dialogue will be removed, but the code considers that the dialogue is displayed: if (myDialog! = null && myDialog.isShowing ()) return;Oldie
K
7

You can create a global flag (boolean) that is set to true if a dialog is shown? If the user click ok, yes, no or anything the dialog is closed and you set the flag to false.

So something like:

boolean dialogShown;

If(dialogShown)
{
  return;
}
else
{
  dialogShown = true;
  dialog =  new AlertDialog.Builder(context);              
  dialog.show();
}
Kurtiskurtosis answered 24/9, 2012 at 8:7 Comment(1)
always put blocks in brackets, even one-liners. You save yourself a lot of troubles (especially when using languages with preprocessor and macros)Bramblett
A
1

For every push on the button you call the method. So this is why it is shown multile times.

The easiest way is to just define an instance variable in your class of your code like:

boolean alertIsBeingShown = false;

Then set it to true when the alert is being shown like this

button.setOnClickListener(new OnClickListener() {
           @Override
        public void onClick(View v) {
               if (alertIsBeingShown) return;
               alertIsBeingShown = true;
               dialog =  new AlertDialog.Builder(context);              
               dialog.show();

    }
 });

and set the variable to false in the code where you press the OK to make it disappear.

Artichoke answered 24/9, 2012 at 8:6 Comment(2)
Why are you using YES and NO?Indulge
too much objective-c I guessPunchy
C
0

Create a positive or negative button for it and just call it as OK and use it to dismiss. Something like :

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
       .setCancelable(false)
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });
AlertDialog alert = builder.create();
alert.show();
Cacology answered 24/9, 2012 at 8:6 Comment(0)
D
0

When I came across this problem, I was not able to use Flags. I had to show a dialog for a clicked list item in a RecyclerView.

In the onclick method I created a variable for the dialog and then when building the dialog I enclosed it with an if statement that checks if the AlertDialog variable is null. When the user clicks on a list item the first time the dialog appears, because the variable is null, even if the user clicks on a item twice only one dialog will appear, because after the second click the AlertDialog variable is no longer null. When the user dismissed the AlertDialog the variable is set to null again.

AlertDialog alertDialog;

if(alertDialog == null) {

            alertDialog = new AlertDialog.Builder(MyActivity.this)
                    .setTitle("Title for Dialog")
                    .setMessage("Dialog Message")
                    .setPositiveButton("Okay", null)
                    .setNegativeButton("No", null)
                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialogInterface) {

                            alertDialog = null;

                        }
                    })
                    .show();
        }
Dielectric answered 8/7, 2019 at 12:38 Comment(0)
U
0
boolean dialogShown;

if (!dialogShown) {

  dialogShown = true;
  dialog =  new AlertDialog.Builder(context);  
  dialog .setNegativeButton(...)
  dialog .setCancelable(false);
  dialog.show();
}

Inside negative click:

dialogShown=false;
Ultann answered 21/2, 2020 at 2:55 Comment(0)
D
-1

Create the dialog in a try-catch-block like this:

    try {
        dialog.setVisible(true);
    } catch (NullPointerException e) {
        dialog =  new AlertDialog.Builder(context);              
        dialog.show();
    }

The first time you execute this, the NullPointerException is thrown and the dialog is created. The following times nothing visible will really happen.

Discommon answered 13/6, 2013 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.