旋转时调用DatePickerDialog onDateSet

时间:2012-12-19 18:54:51

标签: android android-fragments android-lifecycle

我正在使用DialogFragment在onCreateDialog()中返回DatePickerDialog。我已将dateSetListener设置为DialogFragment(下面的示例中为“this”),除了在屏幕旋转发生时调用onDateSet()时,一切正常,这是不可取的。如何在旋转屏幕时让onDateSet不被调用?

我的DialogFragment

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");
}

6 个答案:

答案 0 :(得分:9)

onDateSet方法中,使用Activity::isChangingConfigurations检查是否因配置更改而重新启动Activity。如果是,则不要显示Toast

@Override
public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    if( ! this.getActivity().isChangingConfigurations() ) {
        // Dialog is dismissed by user explicitly, hence show the Toast message.       
        Toast.makeText(getActivity(), "Year: "+year+" Month: "+monthOfYear+" Day: "+dayOfMonth, Toast.LENGTH_SHORT).show();
    }
}

我已经对它进行了测试并且完美无缺。如果需要任何进一步的帮助,请告诉我。

答案 1 :(得分:3)

您可以尝试在对话框的附加/分离回调中使用标记。想法是在对话框尚未附加到窗口时取消任何触发器的触发(因此无法从用户那里进行更改),就像恢复一样(我认为是再次调用侦听器的原因) ):

// a field in the DateDialogFragment
private boolean mShouldBeCanceled = true; // cancel the listener when this is true(the default)

//...
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
     return new DatePickerDialog(getActivity(), this, 2012, 11, 19) {

                @Override
                public void onAttachedToWindow() {
                    mShouldBeCanceled = false;
                }

                @Override
                public void onDetachedFromWindow() {
                    mShouldBeCanceled = true;
                }
     };
}

并在侦听器中使用该标志(由于侦听器仅在构造函数中初始化):

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
    int dayOfMonth) {
    if (mShouldBeCanceled) {
            return; // not a valid listener triggering
    }
    //This is called when screen rotated, which I dont want
    Toast.makeText(getActivity(), "Year: "+year+" Month: "+monthOfYear+" Day: "+dayOfMonth, Toast.LENGTH_SHORT).show();
}

答案 2 :(得分:2)

实施DatePickerDialog的方式是,如果DataSet已注册,它将通知onStop OnDateSetListener中的更改。

当显示onDateSet时,按下后退键时,您还可以看到DatePickerDialog被调用(不仅是方向)

原因是,在创建DatePickerDialog实例时,您已注册了回调侦听器。

因此,最好的方法是创建custom Dialog,其DatePickerDialog延伸并覆盖onStop

自定义DatePickerDialog

  private static class CustomDatePickerDialog extends DatePickerDialog{

    public CustomDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
    }

    @Override
    protected void onStop() {
        return;
    }
}

最后,将CreateDialog更改为

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new CustomDatePickerDialog(getActivity(), this, 2012, 11, 19);
    }

这应该完美。如果您发现任何问题,请与我们联系。

答案 3 :(得分:1)

尝试替换

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
         return new DatePickerDialog(getActivity(), this, 2012, 11, 19);
    }

通过

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
     super.onCreateDialog(savedInstanceState);
 }

答案 4 :(得分:0)

将此属性添加到清单

中的活动
android:configChanges="keyboardHidden|orientation"

答案 5 :(得分:-1)

当旋转屏幕时,活动将被销毁并重新创建,每次都会创建一个新片段。避免此问题的一种方法是在代码中更改if(getActivity()!= null)语句的位置。将它放在不受活动重新创建影响的地方(不在OnCreate方法中)。