Prevent dialog to be opened multiple times when resuming activity

坚强是说给别人听的谎言 提交于 2019-12-13 13:04:36

问题


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.


回答1:


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



回答2:


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.




回答3:


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:


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



回答5:


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




回答6:


I suggest you use AlertDialog and use DialogFragment as google team suggested it. [Dialog developer guide][1]

The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:

AlertDialog
A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

DatePickerDialog or TimePickerDialog A dialog with a pre-defined UI that allows the user to select a date or time.

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



来源:https://stackoverflow.com/questions/8135195/prevent-dialog-to-be-opened-multiple-times-when-resuming-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!