android - 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");}
如何在我调用它的当前片段之上实现我的对话框?
解决方案
尝试使用getParentFragment().getParentFragmentManager()
推荐阅读
- mysql - 添加 INDEX 以加快查询的最佳解决方案是什么?
- r - 如何制作一个计算 R 中 f(x) = exp(x) 的牛顿商的函数?
- deployment - 更新 local_settings.py 后出现 500 个错误
- javascript - Django 和 navigator.serviceWorker.register 仅在本地工作
- typescript - 如何将具有泛型的函数类型应用于函数
- apache-kafka - 远程客户端使用时卡夫卡出错
- c# - 将 TimeSpan 格式化为小时、分钟和秒
- api - 如何从 API 获取亚马逊价格?
- java - 有什么用
多次表达? - amazon-web-services - AWS Glue 连接错误(Glue 到 MySql RDS)