在此处的 Android 文档中:http: //developer.android.com/guide/components/fragments.html Fragment 实现了一个接口。
在 onAttach() 回调中,似乎将当前 Activity 转换为接口。从概念上讲,这怎么可能,并且在 vanilla Java 中是相同类型的转换标准实践吗?
public static class FragmentA extends ListFragment {
// Container Activity must implement this interface
public interface OnArticleSelectedListener {
public void onArticleSelected(Uri articleUri);
OnArticleSelectedListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
}
}
...
}