androidcustomdialog

How do I show only one Dialog at a time?


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();
    }
});

Solution

  • 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();
    }