How to properly retain a DialogFragment through rotation?

后端 未结 3 425
我寻月下人不归
我寻月下人不归 2020-12-02 07:32

I have a FragmentActivity that hosts a DialogFragment.

The DialogFragment perform network requests and handles Facebook authentication, so I need to retain it during

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 07:43

    This is a convenience method using the fix from antonyt's answer:

    public class RetainableDialogFragment extends DialogFragment {
    
        public RetainableDialogFragment() {
            setRetainInstance(true);
        }
    
        @Override
        public void onDestroyView() {
            Dialog dialog = getDialog();
            // handles https://code.google.com/p/android/issues/detail?id=17423
            if (dialog != null && getRetainInstance()) {
                dialog.setDismissMessage(null);
            }
            super.onDestroyView();
        }
    }
    

    Just let your DialogFragment extend this class and everything will be fine. This becomes especially handy, if you have multiple DialogFragments in your project which all need this fix.

提交回复
热议问题