我没有找到解决办法,但我找到了一个“解决办法”。而不是使用
onAttach()
public class ExampleFragment extends Fragment {
private ExampleFragmentListener exampleFragmentListener;
...
//@Override
//public void onAttach(Context context) {
// super.onAttach(context);
// try {
// exampleFragmentListener = (ExampleFragmentListener) context;
// } catch (ClassCastException e) {
// throw new ClassCastException(context.toString() + " must implement ExampleFragmentListener");
// }
//}
public void setExampleFragmentListener(ExampleFragmentListener exampleFragmentListener) {
this.exampleFragmentListener = exampleFragmentListener;
}
...
}
然后,您应该已经在主机活动中实现了ExampleFragmentListener。Just call
setExampleFragmentListener(ActivityOrClassThatImplementsExampleFragmentListener)