防止在恢复活动时多次打开对话框 [英] Prevent dialog to be opened multiple times when resuming activity

查看:59
本文介绍了防止在恢复活动时多次打开对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中,为了询问用户是否要恢复当前游戏,我在主要游戏活动上显示一个对话框,显示您是否要恢复当前游戏?是-否"./p>

问题是,如果我在不回答对话框的情况下重复进行多次此活动,那么我会在彼此之上得到多个对话框,这显然不是我的目标.

我可以使用布尔值var轻松避免这种行为,但是我想知道Dialog类是否具有防止重复的选项或类似的类型.

解决方案

您可以使用单例模式,大致如下:

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

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.

解决方案

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

这篇关于防止在恢复活动时多次打开对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆