10

In my Android application, in order to ask the user if he/she wants to resume a current game, I display a dialog saying "Do you want to resume current game? Yes - No" on the main game activity.

The thing is that if I resume various times this activity without answering the dialog, then I get several dialogs, on top of each other, which is obviously not my goal.

I could easily avoid this behavior using a Boolean var, but I was wondering if the Dialog class had a kind of option preventing to be duplicated or something of the kind.

Janusz
  • 187,060
  • 113
  • 301
  • 369
thomaus
  • 6,170
  • 8
  • 45
  • 63
  • 2
    you can try dialog.isShowing() – Rasel Nov 15 '11 at 10:56
  • when you exit app and open again, the activity is restarted and, i assume, because of your code formation, dialog is restarted again, but maybe been never closed before.. maybe if you put onPause(){ Dialog.dismiss();} it couldhelp your issue –  Nov 15 '11 at 10:59
  • `isFinishing()` ? – Yousha Aleayoub Aug 30 '16 at 23:07

6 Answers6

8

You can use singleton pattern, could be roughly like this:

Dialog myDialog = null;


public void showDialog() {
    if(myDialog == null) {
        /* show your dialog here... */
        myDialog = ...
    }
}


public void hideDialog() {
    if(myDialog != null) {
        /* hide your dialog here... */
        myDialog = null;
    }
}
Caner
  • 57,267
  • 35
  • 174
  • 180
5
private Dialog mDialog;

private void showDialog(String title, String message) {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this)
            .setTitle(title)
            .setMessage(message);

    // Dismiss any old dialog.
    if (mDialog != null) {
        mDialog.dismiss();
    }

    // Show the new dialog.
    mDialog = dialogBuilder.show();
}
4

Rather then doing hacks or using booleans you can use the method given by google itself

public boolean isShowing ()

it Returns a boolean value Whether the dialog is currently showing.

Rahul Aswani
  • 85
  • 2
  • 5
2

I also encountered such a problem when I tried to override the onDismiss() method without super.onDismiss(dialog);

It turns out I deleted super.onDismiss(dialog) and because of this, the dialogs were duplicated

Back added, the error disappeared.

I hope someone will help

DevOma
  • 299
  • 1
  • 4
  • 17
1

Check Dialog already showing or not

private Dialog mDialog;

private void showDialog(String title, String message) {
    //stop multiple dialog window
    if(dialog != null && dialog.isShowing()) {
        return;
    }
    
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this)
            .setTitle(title)
            .setMessage(message);

    dialog = dialogBuilder.show();
}
Boken
  • 4,825
  • 10
  • 32
  • 42
raja a
  • 11
  • 1
0

Use isAdded() method,

Kotlin example:

view.button.setOnClickListener({
            if (!dialog.isAdded) {
                dialogShow(dialog)
            }
        })

and somewhere in the fragment or activity

private fun dialogShow(dialog: DialogFragment?) {
        val fragmentManager: FragmentManager = (context as MyActivity).fragmentManager
        dialog?.show( fragmentManager,TAG)
    }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
alekshandru
  • 169
  • 1
  • 3