【问题标题】:Where is the best place to assign a host listener within a fragment/child fragment?在片段/子片段中分配主机侦听器的最佳位置在哪里?
【发布时间】:2017-07-24 02:30:19
【问题描述】:

我有一个可重复使用的 NumberPickerDialogFragment,可以由活动或片段管理。我读过的每个教程都在 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");
    }
}

显然,如果且仅当片段由活动托管时,这将起作用。但是,如果片段也可以托管在另一个片段中怎么办?我读过 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 android-fragments android-activity android-dialogfragment


    【解决方案1】:

    经过几个月的试验和更多地了解 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());
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-29
      • 1970-01-01
      • 2010-09-13
      • 2014-10-19
      • 2017-07-25
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多