Activity中触发,Fragment中响应
Activity中:
private OnClickActivityListener mOnClickActivityListener;
public interface OnClickActivityListener {
void onClickActivity();
}
@Override
public void onAttachFragment(Fragment fragment) {
try {
mOnClickActivityListener = (OnClickActivityListener) fragment;
} catch (Exception e) {
}
super.onAttachFragment(fragment);
}
if(mOnClickActivityListener!=null){
mOnClickActivityListener.onClickActivity();
}
Fragment中触发,Activity中响应
Fragment中:
private OnFragmentInteractionListener mListener;
public interface OnFragmentInteractionListener {
void onFragmentInteraction();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
if (mListener != null) {
mListener.onFragmentInteraction();
}
然后在另一个中实现接口,在接口方法中写具体实现
感谢https://blog.csdn.net/menwaiqingshan/article/details/83856493