OnTimeSet called also when dismissing TimePickerDialog

Alessandro Roaro picture Alessandro Roaro · Oct 18, 2013 · Viewed 7.1k times · Source

Today I was trying to use the TimePickerDialog but I noticed a couple of flaws.

  1. OnTimeSet is called also when the dialog is dismissed (by clicking outside, for example)
  2. OnTimeSet is called twice when the user taps the "Done" button

The API I'm using is 18.

Anyone else has experienced these problems? How did you solve them?

Answer

Ankur Chaudhary picture Ankur Chaudhary · Sep 25, 2014

You hould use already given method of View class:

new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hour, int minute) {  
        if (view.isShown()) {
            // This method will return true only once...
        }
    }
};