Android最佳实践中,关于Fragment之间的交互(详见此处和此处)要求Activity实现由子Fragment定义的监听器。然后Activity负责管理片段之间的通信。
据我了解,这是为了使片段之间保持松耦合。然而,
据我了解,这是为了使片段之间保持松耦合。然而,
Is this also the case for nested fragments? I can imagine it might make sense for a nested fragment to report directly to it's parent fragment instead of the Activity.
If a nested fragment has its parent fragment implement it's listener, how would one (or should one) require the parent fragment to do this. In other words, is a similar to the paradigm to the following but for
Fragments
:@Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (OnHeadlineSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } }
onAttach()
中附加监听器,并在不是“Activity”的情况下抛出错误。 - bcorsosetListener(...)
,但这也将限制 Fragment 只能与Activity
或Fragment
一起使用,而不能同时使用。最好的方法可能是完全放弃它。 - bcorso