Catching Cancellations or Dismissals for TimePicker

I am not sure how to implement the onCancel / onDismiss listener for the code below. Can anyone help.

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    mHour = hourOfDay;
                    mMinute = minute;
                    time = ""+mHour+":"+mMinute;
                    notifications();
                }
    };



@Override
   protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, mHour, mMinute, false);
        }
        return null;
    }

Edit: working code is below. When you click the Install button, it does this. Anything else removed the flag.

        @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            TimePickerDialog myTime = new TimePickerDialog(this,mTimeSetListener, mHour, mMinute, false);
            myTime.setOnCancelListener(new OnCancelListener(){
                CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
                public void onCancel(DialogInterface dialog) {
                    Log.d("IN HERE","HERE2");
                    cbChecked = false;
                    notValue = 0;
                    checkBox.setChecked(false);
                    checkBox.setText("Use reminders");
                }
            });
            return myTime;
        }
        return null;
    }
+3
source share
1 answer

Instead of returning TimePickerDialogdirectly as you are in a method onCreateDialog, you should instantiate it and add a listener onCancel.

...
case TIME_DIALOG_ID:
   TimePickerDialog myTime = new TimePickerDialog(this,yourListener, h, m, false);
   myTime(new DialogInterface.OnCancelListener(){
      @Override
      public void onCancel(DialogInterface arg0) {
        //your stuff
      }
   );//For cancel button

   return myTime; //Return the dialog
break;
...
+1
source

All Articles