Prevent the dialog from opening several times when activity resumes

In my Android application, to ask the user if he wants to resume the current game, I show the dialog “Do you want to resume the current game? Yes - No” in the main game.

The fact is that if I resume various actions without responding to the dialogue, I get several dialogues on top of each other, which is clearly not my goal.

I could easily avoid this behavior with Boolean var, but I was wondering if the Dialog class had some kind of option that prevents duplication or something like that.

+6
source share
6 answers

, :

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;
    }
}
+6

, , , Google

public boolean isShowing ()

it .

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

isAdded(),

Kotlin:

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

-

private fun dialogShow(dialog: DialogFragment?) {
        val fragmentManager: FragmentManager = (context as MyActivity).fragmentManager
        dialog?.show( fragmentManager,TAG)
    }
0

, onDismiss() super.onDismiss(dialog);

super.onDismiss(dialog) -

, .

-

0

AlertDialog DialogFragment, google. [ ] [1]

Dialog , . :

AlertDialog
, , , .

DatePickerDialog TimePickerDialog , .

These classes define the style and structure of your dialog, but you should use DialogFragment as a container for your dialog. The DialogFragment class provides all the controls necessary to create your dialog and control its appearance , instead of calling methods in the Dialog object.

-1
source

All Articles