首页 > 解决方案 > ParentFragmentManager 和 ChildFragmentManager 给出错误的行为

问题描述

我有一个片段,它是父片段的众多子片段之一。另外,我有一个通过方法 show(FragmentManager, tag) 调用的自定义警报对话框的实例。

问题是当警报对话框出现在当前片段上方时,我无法实现所需的行为。如果我使用 getParentFragmentManager 或 getSupportFragmentManager 它会在层次结构中向上的父片段上方打开,当我使用 getChildFragmentManageк 时,它会显示一秒钟然后消失。

尝试了我遇到的所有东西后,我几乎迷路了。将非常感谢任何帮助。

这是我的自定义警报对话框的一部分

public class CustomAlertDialog extends BaseDialogFragment {
    private AlertdialogBinding mBinding;
    private final OnDialogClickListener mDialogClickListener;

    public CustomAlertDialog (OnDialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface OnDialogClickListener {
        void onPositiveClick(CustomAlertDialog dialog);

        void onNegativeClick(CustomAlertDialog dialog);

        void onClick(CustomAlertDialog dialog, int which);

    }

    public static CustomAlertDialog newInstance(String title, String message, Boolean positive, Boolean negative, OnDialogClickListener listener) {
        CustomAlertDialog fragment = new CustomAlertDialog(listener);
        Bundle args = new Bundle();
        args.putString(ALERT_DIALOG_TITLE_KEY, title);
        args.putString(ALERT_DIALOG_MESSAGE_KEY, message);
        args.putBoolean(ALERT_DIALOG_BUTTON_OK_KEY, positive);
        args.putBoolean(ALERT_DIALOG_BUTTON_CANCEL_KEY, positive);
        fragment.setArguments(args);
        return fragment;
    }

这就是我从我的片段中调用它的方式,它是父片段的子片段

 CustomAlertDialog customAlertDialog  = CustomAlertDialog.newInstance(getResources().getString(R.string.dialog_title), getResources().getString(R.string.alert), true, false, new CustomAlertDialog.OnDialogClickListener() {

                                @Override
                                public void onPositiveClick(CustomAlertDialog dialog) {
                                    return;
                                }

                                @Override
                                public void onNegativeClick(CustomAlertDialog  dialog) {

                                }

                                @Override
                                public void onClick(CustomAlertDialog  dialog, int which) {

                                }
                            });
                            customAlertDialog .show(getParentFragmentManager(), "dialog");}

如何在我调用它的当前片段之上实现我的对话框?

标签: androidandroid-fragmentsandroid-alertdialogfragmentmanager

解决方案


尝试使用getParentFragment().getParentFragmentManager()


推荐阅读