Reputation: 753
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.
Specific Scenario
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 code
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;
}
}
}
};
Upvotes: 0
Views: 765
Reputation: 977
You have to dismiss the previous the shown dialog before showing the next one.
please check this code and it is working fine as per your requirement
public class MainActivity extends AppCompatActivity {
private MaterialAlertDialogBuilder materialDialog;
private AlertDialog dialog;
private String title;
private String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
materialDialog = new MaterialAlertDialogBuilder(this);
}
@Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(wifiStateReceiver, intentFilter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(wifiStateReceiver);
}
private final 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:
title = "Connected";
msg = "Wifi Enabled.";
materialDialog.setPositiveButton("ok", (dialogInterface, i) -> {
dialogInterface.dismiss();
}).setNegativeButton("close", (dialogInterface, i) -> {
dialogInterface.dismiss();
showToast(msg);
});
break;
case WifiManager.WIFI_STATE_DISABLED:
title = "disconnected";
msg = "Wifi Disabled.";
materialDialog.setPositiveButton("ok", (dialogInterface, i) -> {
dialogInterface.dismiss();
}).setNegativeButton("close", (dialogInterface, i) -> {
dialogInterface.dismiss();
showToast(msg);
});
break;
}
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
dialog = materialDialog.setTitle(title).setMessage(msg).create();
dialog.show();
}
};
private void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
Upvotes: 1
Reputation: 1141
Just call cancel
before reassigning alertDialog
:
...
alertDialog.cancel(); // add this line
alertDialog = builder.create();
...
Upvotes: 0