DialogFragment简单封装及遇到的问题

DialogFragment 是android3.0以后推出的,用来替代Dialog,它其实就是一个fragment,具有fragment的所有生命周期,比Dialog更好管理。

简单的封装一下:

public abstract class BaseDialogFragment extends DialogFragment {

    protected Dialog dialog = null;
    private String TAG = "BaseDialogFragment";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayoutId(), container, false);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_FRAME, R.style.BaseDialog);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        dialog = super.onCreateDialog(savedInstanceState);
        //点击弹出框外面是否可取消
        dialog.setCanceledOnTouchOutside(false);
        //点击返回键是否可取消
        dialog.setCancelable(true);
        return dialog;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        //设置dialogFragment位置及属性
        if (null != dialog) {
            Window window = dialog.getWindow();
            if (null != window) {
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.MATCH_PARENT;
                window.setAttributes(lp);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 绑定布局
     *
     * @return int
     */
    protected abstract int getLayoutId();


    @Override
    public void show(FragmentManager fm, String tag) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(this, tag);
        // 这里把原来的commit()方法换成了commitAllowingStateLoss()
        // 解决Can not perform this action after onSaveInstanceState with DialogFragment
        ft.commitAllowingStateLoss();
        //解决java.lang.IllegalStateException: Fragment already added
        fm.executePendingTransactions();
    }

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

    /**
     * 关闭弹出框
     */
    protected void hideDialog() {
        try {
            //没初始化就会出现问题
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 短时间显示Toast
     *
     * @param msg 消息体
     */
    protected void toastShort(String msg) {
//        ToastUtils.showToast(getContext(), msg);
    }

    /**
     * 短时间显示Toast
     *
     * @param resId 消息体
     */
    protected void toastShort(int resId) {
//        ToastUtils.showToast(getContext(), resId);
    }

}

使用也很简单,继承BaseDialogFragment,复写相关方法,填写自己的逻辑。
这里就不多说,主要再说一下show的情况:

//做一下判断, dialog是否已经被添加
if (dialogFragment != null && !dialogFragment.isAdded()) {
    dialogFragment.show(TAG,getSupportFragmentManager());
}

遇到的问题:

1、Can not perform this action after onSaveInstanceState with DialogFragment

解决方法:
//复写show方法,把commit() 改成 commitAllowingStateLoss()
@Override
public void show(FragmentManager fm, String tag) {
	 FragmentTransaction ft = fm.beginTransaction();
     ft.add(this, tag);
     // 这里把原来的commit()方法换成了commitAllowingStateLoss()
     // 解决Can not perform this action after onSaveInstanceState with DialogFragment
     ft.commitAllowingStateLoss();
}

2、java.lang.IllegalStateException: Fragment already added
解决方法:

//1、在dialog show之前判断dialog是否被添加
if (dialogFragment != null && !dialogFragment.isAdded()) {
    dialogFragment.show(TAG,getSupportFragmentManager());
}

//2、复写show方法,在dialogshow出来后写上fm.executePendingTransactions();
@Override
public void show(FragmentManager fm, String tag) {
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(this, tag);
    // 这里把原来的commit()方法换成了commitAllowingStateLoss()
    // 解决Can not perform this action after onSaveInstanceState with DialogFragment
    ft.commitAllowingStateLoss();
    //解决java.lang.IllegalStateException: Fragment already added
    fm.executePendingTransactions();
}

你可能感兴趣的:(原创,android)