gpt4 book ai didi

android - 在 fragment/子 fragment 中分配主机监听器的最佳位置在哪里?

转载 作者:行者123 更新时间:2023-11-29 02:39:26 26 4
gpt4 key购买 nike

我有一个可重复使用的 NumberPickerDialogFragment,它可以由 Activity 或 fragment 管理。我读过的每个教程都会在 onAttach(Context) 覆盖中分配监听器。像这样:

@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (Listener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement Listener");
}
}

显然,如果且仅当 fragment 由 Activity 托管时,这才有效。但是,如果 fragment 也可以托管在另一个 fragment 中怎么办?我读到过 onCreateViewonViewCreatedonActivityCreated 都适用于这种情况。像这样:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
try {
listener = (Listener) getParentFragment();
} catch (ClassCastException e) {
throw new ClassCastException(getParentFragment().toString()
+ " must implement Listener");
}
return super.onCreateView(inflater, container, savedInstanceState);
}

所以上面的代码都涵盖了其中一种情况,或者不是两种情况。现在,由于我的 Fragment 是从 DialogFragment 扩展而来的,所以我有以下代码:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

Fragment fragment = getParentFragment();

if (fragment != null) {
try {
listener= (Listener) fragment;
} catch (ClassCastException e) {
throw new ClassCastException(fragment.toString()
+ " must implement Listener");
}
} else {
Activity activity = getActivity();
try {
listener= (Listener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement Listener");
}
}

我只是担心这可能不是最好的方法,考虑到我见过的所有教程都在 onAttach() 覆盖中这样做。

那么我的问题是:

  • 如果 onAttach 是分配主机监听器的最佳位置,其中监听器是 Activity,并且
  • onCreateView 是分配主机监听器的最佳位置,其中监听器是父 Fragment
  • 那么分配主机监听器的最佳位置在哪里,监听器既可以是 Activity 也可以是父 Fragment

最佳答案

经过几个月的试验和对 Android 的更多了解后,我找到了解决这个特殊难题的方法。

对我来说最好的地方是始终在 OnAttach() 方法中管理它。

调用 OnAttach() 时,我会在此处调用 getParent() 以确认此 fragment 是将自身直接附加到 Activity 还是 fragment 。查看示例代码:

@Override
public void onAttach(Context context) {
super.onAttach(context);

if (getParentFragment() == null) {
try {
mController = (Controller) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement " + Controller.class.getName());
}
} else {
try {
mController = (Controller) getParentFragment();
} catch (ClassCastException e) {
throw new ClassCastException(getParentFragment().getClass().toString()
+ " must implement " + Controller.class.getName());
}
}
}

关于android - 在 fragment/子 fragment 中分配主机监听器的最佳位置在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45271783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com