Avoid DatePickerDialog calls onDateSet two times

DatePickerDialog

While working with DatePickerDialog, I see an issue when setting a date and click done button. Its calling onDateSet method two times.

DatePickerDialog

here is a simple solution for it. DatePicker parameter object has a method called isShown(). Place method statements in the if condition of isShown(), which avoids calling onDateSet method statements two times.

@Override 
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    if(view.isShown()) {
      //method statements 
       Log.i(MyClassName.class.getName(), year+ "/" +      monthOfYear   + "/" + dayOfMonth); 
    }
}

ref & credits to link.

Cheers.. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *