第二次调用后,MaterialDialog不会显示

时间:2017-05-15 22:56:37

标签: android android-dialog

我有一些问题需要理解为什么这个,一般来说,名为Material Dialogs的伟大图书馆让我失望:

private MaterialDialog wifiDialog;

@Override
public void showWiFiDialog() {

    if (wifiDialog != null) {
        wifiDialog.dismiss();
        wifiDialog = null;
    }

    wifiDialog = new MaterialDialog.Builder(this)
            .title("Please activate WiFi")
            .contentColor(Color.BLACK)
            .content(R.string.wifi_dialog_content)
            .positiveText("Settings")
            .positiveColor(Color.BLACK)
            .negativeText("Exit")
            .negativeColor(Color.BLACK)
            .cancelable(false)
            .onPositive((dialog, which) -> startActivityForResult(new Intent(ACTION_WIFI_SETTINGS), REQUEST_CHECK_SETTINGS))
            .onNegative((dialog, which) -> finish())
            .build();

    wifiDialog.show();
}

@Override
public void hideWifiDialog() {
    if (wifiDialog != null) {
        wifiDialog.dismiss();
        wifiDialog = null;
    }
}

致电:

  1. showWiFiDialog() - >按预期显示
  2. hideWifiDialog() - >按预期隐藏
  3. showWifiDialog() - >有时候,有点运气
  4. hideWifiDialog() - >有时,但需要更多的运气
  5. 总结:第一次调用show和hide时,一切都按预期工作。无论何时多次调用它,行为都变得不确定。我想知道为什么?

1 个答案:

答案 0 :(得分:0)

我认为,每次要显示时,都不需要重新初始化MaterialDialog。让我们看看下面的代码是否有所不同。

private MaterialDialog wifiDialog;

@Override
public void showWiFiDialog() {

    if (wifiDialog != null) {
        wifiDialog.dismiss();
    }else{

    wifiDialog = new MaterialDialog.Builder(this)
            .title("Please activate WiFi")
            .contentColor(Color.BLACK)
            .content(R.string.wifi_dialog_content)
            .positiveText("Settings")
            .positiveColor(Color.BLACK)
            .negativeText("Exit")
            .negativeColor(Color.BLACK)
            .cancelable(false)
            .onPositive((dialog, which) -> startActivityForResult(new Intent(ACTION_WIFI_SETTINGS), REQUEST_CHECK_SETTINGS))
            .onNegative((dialog, which) -> finish())
            .build();
    }

    wifiDialog.show();
}

@Override
public void hideWifiDialog() {
    if (wifiDialog != null) {
        wifiDialog.dismiss();
    }
}