旋转屏幕后如何关闭对话碎片

时间:2014-04-30 09:51:43

标签: android android-fragments android-dialog android-dialogfragment

我已经实现了一个dialogFragment,它显示了一条简单的消息:

private static DialogFragment loadingDialogFragment = null;

public void showLoadingDialog(String title, String message) {
    loadingDialogFragment = new LoadingDialogFragment();
    Bundle args = new Bundle();
    args.putString("title", title);
    args.putString("message", message);
    loadingDialogFragment.setArguments(args);
    loadingDialogFragment.show(fragManager, "loadingDialog");
}

public void dismissLoadingDialog() {
    if (loadingDialogFragment != null) {
        loadingDialogFragment.dismiss();
    }
}

但是如果显示对话框并且我旋转屏幕,那么我的dissmisslaodingDialog不起作用。我想这是因为当你旋转它再次调用on create函数并且它失去了对话框的引用,但我已经使它静止。

上面的代码放在一个Utility类中,我从我的活动中调用它,如下所示:

util.showLoadingDialog("Loading", "");
SendCommand(deviceId, "Command");   

然后我在SendCommmand的回调函数中调用utils.dismissLoadingDialog

我在这里缺少什么?

{编辑}

我的activityFragment中的代码

util.showLoadingDialog("Loading", "");
SendCommand(deviceId, "Command");

public void SendCommand(int deviceId, final String command) {

        Network.get(mContext, "/" + command + "/" + deviceId, null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Log.i(TAG + " sendPanelCommand", "recieved ok response to command: " + command);                
                util.dismissLoadingDialog();
            }

             @Override
             public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                util.dismissLoadingDialog();                    
                util.showErrorDialog(
                        "Error",
                        "An error has occored while trying to issue the command to the panel. This can be caused by network issues or your panel sending out priority information such as alarms. Please try again.");
            }

        });
    }
}

Utils Class:

public class Utils {

private Context context;
private FragmentManager fragManager;

public Utils(Context context) {
    this.context = context;
}

public Utils(Context context, FragmentManager fragman) {
    this.context = context;
    this.fragManager = fragman;
}
...

private DialogFragment loadingDialogFragment = null;

public void showLoadingDialog(String title, String message) {
    loadingDialogFragment = new LoadingDialogFragment();
    Bundle args = new Bundle();
    args.putString("title", title);
    args.putString("message", message);
    loadingDialogFragment.setArguments(args);
    loadingDialogFragment.show(fragManager, "loadingDialog");
}

public void dismissLoadingDialog() {
    if (loadingDialogFragment != null) {
        loadingDialogFragment.dismiss();
    } else {
        // the reference is null, try the FragmentManager to see if it
        // wasn't
        // automatically restored
        DialogFragment dg = (DialogFragment) fragManager.findFragmentByTag("loadingDialog");
        if (dg != null) {
            // this reference isn't null so the dialog is available
            dg.dismiss();
        }
    }
}   
}

2 个答案:

答案 0 :(得分:3)

在屏幕旋转时取消DialogFragment。几分钟前我在搜索并尝试解除和处理旋转的所有变量后发现的是DialogFragment的onPause方法在旋转期间被调用,你可以在那里将它解雇。

public static class ErrorDialogFragment extends DialogFragment {
    // Global field to contain the error dialog
    private Dialog mDialog;

    public void onPause(){
        super.onPause();
        this.dismissAllowingStateLoss();
    }

    static ErrorDialogFragment newInstance() {
        ErrorDialogFragment d = new ErrorDialogFragment();
        return d;
    }
 ...
}

答案 1 :(得分:2)

要限制对话框重新创建,您可以在对话框中覆盖onCreate方法:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) dismiss();
}