安卓 |如何在显示下一个对话之前关闭上一个对话? [英] Android | How to dismiss the previous dilogue before displaying next dilogue?

查看:42
本文介绍了安卓 |如何在显示下一个对话之前关闭上一个对话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有针对不同条件的多个对话框.当我为一种情况显示对话时,然后我尝试为另一种情况显示另一种对话.它与前一个重叠.

I have multiple dilogue boxes for different different conditions. when I a dilogue is displayed for one condition and then I try to display another one for another condition. It overlaps the previous one.

特定场景

我有两个对话.一种用于 wifi 未激活的情况.另一种是当wifi处于活动状态时.假设当我启动应用程序时,wifi 未处于活动状态.所以,它显示了一个对话.然后我打开了wifi.它展示了另一个对话.但是,它与前一个重叠.在下面的屏幕截图中,通过查看对话框的阴影可以清楚地看到.左一是当wifi打开时.然后我关闭了wifi并再次打开.而这种重叠发生了.

I have two dialogues. One for when wifi is not active. Another one is when wifi is active. Suppose When I launched an app wifi was not active. So, it displayed a dilogue. Then I turned on wifi. It displayed another dilogue. But, It overlapped previous one. In below screenshot it is clearly visible by looking at the shadow of dilogue box. Left one is when wifi is on. And then I turned off the wifi and turned on again. And this overlapping happened.

屏幕截图

Java 代码

 private BroadcastReceiver wifiStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int wifiStateExtra = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                    WifiManager.WIFI_STATE_UNKNOWN);
            switch (wifiStateExtra) {


                case WifiManager.WIFI_STATE_ENABLED:
                    //WIFI ENABLED

                    {
                    info = wifiManager.getConnectionInfo();
                    if (!info.getSSID().trim().equals(ssid)) {
                        title = "Connect to Devil";
                        msg = "In order to work with system you need to be connected with Devil";

                        builder.setTitle(title);
                        builder.setMessage(msg);
                        builder.setPositiveButton("OK", (dialog, which) -> startActivity(WiFiIntent));
                        builder.setNegativeButton("Close App", ((dialog, which) -> finishAffinity()));
                        alertDialog = builder.create();
                        alertDialog.show();
                        alertDialog.setCancelable(false);

                    }

                    break;
                }


                case WifiManager.WIFI_STATE_DISABLED:
                    //WIFI DISABLED
                {
                    title = "Enable WiFi";
                    msg = "Please enable wifi and connect to Devil";
                    builder.setTitle(title);
                    builder.setMessage(msg);
                    builder.setPositiveButton("OK", (dialog, which) -> startActivity(WiFiIntent));
                    builder.setNegativeButton("Close App", ((dialog, which) -> finishAffinity()));
                    alertDialog = builder.create();
                    alertDialog.show();
                    alertDialog.setCancelable(false);
                    break;
                }
            }
        }
    };

推荐答案

在重新分配alertDialog之前调用cancel:

...
alertDialog.cancel(); // add this line
alertDialog = builder.create();
...

这篇关于安卓 |如何在显示下一个对话之前关闭上一个对话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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