我找到了更好的解决方案。 getCurrentFragment()
无法正确实施,我可以告诉。
我的代码先前启动了一个对话框,然后回调到由onAttach对话框存储的Activity。该活动然后需要找到正确的片段,这是有问题的。
正确的解决办法是,先呼叫setTargetFragment()的新的对话框片段:
SelectProblemDialogFragment f = SelectProblemDialogFragment.newInstance(args);
f.setTargetFragment(this, 0);
f.show(getFragmentManager(), "select_problem_dialog_fragment");
,然后在onAttach(),简单地使用,作为监听器。
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
mListener = (SelectProblemDialogListener) getTargetFragment();
if (mListener == null)
{
mListener = (SelectProblemDialogListener) activity;
}
} catch (ClassCastException e)
{
throw new ClassCastException("Must implement SelectProblemDialogListener");
}
}