我正在使用 DialogFragment 在 onCreateDialog() 中返回 DatePickerDialog。我已将 dateSetListener 设置为 DialogFragment (在下面的示例中为“this”),除了在屏幕旋转发生时调用 onDateSet() 之外,一切正常,这是不可取的。如何在屏幕旋转时不调用 onDateSet?
我的对话片段
public static class DateDialogFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener{
public static DateDialogFragment newInstance() {
return new DateDialogFragment();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), this, 2012, 11, 19);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
//This is called when screen rotated, which I dont want
Toast.makeText(getActivity(), "Year: "+year+" Month: "+monthOfYear+" Day: "+dayOfMonth, Toast.LENGTH_SHORT).show();
}
}
这就是我所说的
if(getActivity()!=null){
FragmentManager fm = getActivity().getSupportFragmentManager();
DialogFragment newFragment = DateDialogFragment.newInstance();
newFragment.show(fm, "dialog");
}