为什么Dialogfragment会两次显示警告对话框?

时间:2015-06-07 13:53:09

标签: android android-support-library

我正试图在互联网上显示一个警告对话框。一切正常但对话框显示两次(重叠一个在另一个上)。为什么对话框显示两次,即使我在显示之前检查对话框实例是否为null .Below是我写的代码。

以下是活动中编写的代码。

BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null &&
                activeNetwork.isConnected();
        if (isConnected) {
            internetAvailable();
        } else {
            showNoInternetPopup();
        }
    }
};

protected abstract void internetAvailable();

protected void showNoInternetPopup() {
    SimpleAlertDialog alertDialog = new SimpleAlertDialog();
    alertDialog.show(getSupportFragmentManager(), "1001");
}


@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(networkStateReceiver, filter);
}

这是扩展dialogFragment

的对话框类
public class SimpleAlertDialog extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Your network seems to be unavailable")
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dismiss();
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();

}

@Override
public void show(FragmentManager manager, String tag) {
    if (manager.findFragmentByTag(tag) == null) {
        super.show(manager, tag);
    }
}

}

2 个答案:

答案 0 :(得分:0)

我认为你获得 double 回调的问题构成了操作系统。 记录下来进行双重验证。 如果确实如此,那么你需要自己处理弹出窗口,这样你就不会再玩两次,将警报对话框保存为成员,然后在其上调用 isShown()。当然是chekc,如果它为null。

答案 1 :(得分:0)

通过在onPause上取消注册接收器解决了这个问题。傻了我