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");
}
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With