在官方的範例中有以下做法
http://developer.android.com/training/basics/fragments/communicating.html
//左側Fragment
public class MainLeftFragment extends Fragment {
private OnFragmentInteractionListener mListener;
//點擊事件後如果要傳的值就使用
mListener.onFragmentInteraction("*");
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(String uri);
}
}
//MainActivity
public class MainActivity extends BaseFragmentActivity implements MainLeftFragment.OnFragmentInteractionListener{
//將要發生的事件寫在這
@Override
public void onFragmentInteraction(String uri) {
Toast.makeText(this,uri,Toast.LENGTH_SHORT).show();
}
}