Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatePickerDialog onDateSet called when rotated

I am using a DialogFragment to return a DatePickerDialog in onCreateDialog(). I have set the dateSetListener to be the DialogFragment ("this" in example below) and everything works except that onDateSet() is called when a screen rotation occurs, which is undesirable. How can I get onDateSet to not be called when the screen is rotated?

My 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();
    }

}

And this is how I call it

if(getActivity()!=null){
    FragmentManager fm = getActivity().getSupportFragmentManager();
    DialogFragment newFragment = DateDialogFragment.newInstance();
    newFragment.show(fm, "dialog");
}
like image 602
jfortunato Avatar asked Dec 19 '12 18:12

jfortunato


2 Answers

In onDateSet method, check whether Activity is being restarted due to the configuration change by using Activity::isChangingConfigurations. If yes, then don't display the 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();
    }
}

I've tested it and works perfectly. Let me know if any further help is required.

like image 72
Manish Mulimani Avatar answered Oct 02 '22 00:10

Manish Mulimani


You could try using a flag in the attached/detached callbacks of the dialog. The idea is to cancel any triggering of the listener while the dialog isn't yet attached to the window(so the change can't possible come from the user) like a restore(which I think is the reason for the listener being called again):

// 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;
                }
     };
}

and use the flag in the listener(due to the listener being initialized only in the constructor):

@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();
}
like image 41
user Avatar answered Oct 02 '22 00:10

user