0

I know this has been asked many times, but I can't find an answer to suit my problem.

So I have an activity which has a button where its' text is the current time (the time the activity was accessed). I can click on the button to show a TimePickerDialog where I can change the time, click the 'Set' button and the button updates with the new time. So far, so good, so basic.

My problem is that if I launch the TimePicker, change the time, click the Cancel button and then launch the TimePicker again, the displayed time is the time I cancelled instead of the original time from the button that launched it. If I cancel the TimePicker, I want it to forget any previous data.

Here is my code:

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_DIALOG_ID:
        recordTime = new TimePickerDialog(this,mTimeSetListener, mHour, mMinute, true);
        recordTime.setOnCancelListener(new OnCancelListener(){

            @Override
            public void onCancel(DialogInterface arg0) {

final Calendar c = Calendar.getInstance();
                mHour = c.get(Calendar.HOUR_OF_DAY);
                mMinute = c.get(Calendar.MINUTE);
                recordTime.dismiss();
            }
        });

        recordTime.setOnDismissListener(new OnDismissListener(){
            @Override
            public void onDismiss(DialogInterface arg0) {
                recordTime.cancel();
                recordTime.dismiss();
            }

        });
        return recordTime;

    }
    return null;
}


// the callback received when the user sets the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;
                updateTimeDisplay();
            }
        };
DaveSav
  • 1,364
  • 5
  • 21
  • 41

2 Answers2

2

If I cancel the TimePicker, I want it to forget any previous data.

Each time a Dialog is opened with showDialog() it passes through onPrepareDialog(), simply reset the time here. If the user clicked Set the new time is saved in mHour and mMinute, if the dialog is cancelled for any reason this restores the original data:

@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
    switch (id) {
    case TIME_DIALOG_ID:
        recordTime.updateTime(mHour, mMinute);
    }
}

You can easily call this in the OnCancelListener as well:

recordTime.setOnCancelListener(new OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        recordTime.updateTime(mHour, mMinute);
    }
});

Also your original OnCancel and OnDismiss listeners seem redundant. What are you trying to do with them?

You should know that onDismiss() is called if the user clicks Set, Cancel, or uses the back button. If the user clicks Set you don't want to call the onCancel() method is onDismiss(). Don't treat onCancel() and onDismiss() as the same methods.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • Thanks. I found that just using onPrepareDialog did the trick. Using .setOnCancelListener by itself, didn't restore the original time. – DaveSav Aug 23 '12 at 20:16
  • Can check running code here https://stackoverflow.com/questions/4724781/timepickerdialog-cancel-button/47043247#47043247 as well – Deepti Oct 31 '17 at 19:37
0

You are never reseting your date. If you reset your date/time in the onCancel method, everything should work fine.

ahodder
  • 11,353
  • 14
  • 71
  • 114
  • How would I do that? As a quick test I put in final Calendar c = Calendar.getInstance(); mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE); but that didn't help. – DaveSav Aug 22 '12 at 23:26
  • I've changed my code example to incorporate your suggestion; but it still doesn't work – DaveSav Aug 22 '12 at 23:43