1:Fragment内定义一个接口,或外部定义一个接口,

2:Fragment定义一个接口对象的属性

3:Activity实现这个接口

4:Fragment的onAttach方法中强转Activity为接口

 

例如:

@Override
public void onAttach(Activity activity)
{
  super.onAttach(activity);

  try
  {
    onFragmentChangeListener = (OnFragmentChangeListener)activity;
  }
  catch(ClassCastException e)
  {
    throw new ClassCastException(activity.toString() + " must implement OnFragmentChangeListener");
  }
}