According to your comment, it should be easy enough to protect the OnTimeSetListener with a boolean so that your logic does not start if you click Cancel.
I had a quick game with your code, and the following seemed to work well:
private boolean mIgnoreTimeSet = false; final TimePickerDialog timeDlg = new TimePickerDialog(PreferencesActivity.this, PreferencesActivity.this, hour, min, true); // Make the Set button timeDlg.setButton(DialogInterface.BUTTON_POSITIVE, "Set", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { mIgnoreTimeSet = false; // only manually invoke OnTimeSetListener (through the dialog) on pre-ICS devices if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) timeDlg.onClick(dialog, which); Toast.makeText(getApplicationContext(), "Set", Toast.LENGTH_SHORT).show(); } }); // Set the Cancel button timeDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "Cancel", Toast.LENGTH_SHORT).show(); mIgnoreTimeSet = true; dialog.cancel(); } }); timeDlg.show(); @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { if (mIgnoreTimeSet) return; // get the values here - this will only run if 'Set' was clicked }
For example, I assumed that your PreferencesActivity implements OnTimeSetListener . Since onTimeSet(...) is called after onClick(...) , you can simply toggle a boolean that will determine if something needs to be done with the values in the callback or just ignore them.
I have to agree that this is not ideal, but at least functional. After the holidays, I will try again to look at the "best" solution.
One alternative would be to use reflection to get a handle to the built-in TimePicker widget, but more likely to break in future releases of Android.
// Edit: A Potential Alternative? (not verified)
The second alternative, which may be a bit cleaner: TimePickerDialog and overwrite the public method (empty):
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
There, keep track of the user-specified hour of the day and minute, and implement two recipients that return them. Then, get these values only from the dialog box if the "positive" button is pressed, but just skip the "Cancel" button. Basically, you no longer need the OnTimeSetListener , and therefore it doesn't matter if it is ever called or not.
// Edit2: support for ICS pre-connected devices
After receiving a few comments about the proposed solution that did not work on devices that preceded ICS, I made a small change to the above code - even hard, it did not fit the original question.
The main difference between devices before ICS and post-ICS is that the OnTimeSetListener does not start automatically after the onClick() dialog box method is launched. A simple workaround for this is to call the onClick() method in a dialog box, which then calls the listener. In the above solution, I added version code checking, because otherwise your listener will be called twice on devices after ICS, which can lead to unwanted side effects.
The behavior of Android 2.1-update1 and Android 4.2.2 was tested and confirmed.