这里说一个官方推荐的写法:
private OnFragmentInteractionListener mListener;
@Override
publicvoid onAttach(Context context) {
super.onAttach(context);
if(contextinstanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
}
else {
thrownew RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
publicvoid onDetach() {
super.onDetach();
mListener =null;
}
publicinterface OnFragmentInteractionListener {
// TODO: Update argument type and name void onFragmentInteraction(String data);
}
根据上面的代码,一旦绑定activity,也自然会注册接口,所以只要在activity中实现接口便可以了。
网友评论