javaandroidandroid-studiodatepickerdialogtimepickerdialog

How to disable TimePickerDialog if DatePickerDialog is cancelled?


I am currently making a to-do-list app. I have a reminder function wherein if a user clicks it, then the datepickerdialog and timepickerdialog are shown.

However, if the user presses 'cancel' on the datepickerdialog, then I don't want to show the timepickerdialog. Any help would be appreciated.

Here is my code when the "reminder" button is pressed.

 //When Reminder button clicked, show datepickerdialog and timepickerdialog
    public void openCalendar(View view) {

            final Calendar notifycalendar = Calendar.getInstance();
            notifyyear = notifycalendar.get(Calendar.YEAR);
            notifymonth = notifycalendar.get(Calendar.MONTH);
            notifydayOfMonth = notifycalendar.get(Calendar.DAY_OF_MONTH);


        final DatePickerDialog notifydatePickerDialog = new DatePickerDialog(Editor_Activity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int notifyyear, int notifymonth, int notifydayOfMonth) {

                        String notifyDate;

                        notifyDate = notifydayOfMonth + "/" + (notifymonth + 1) + "/" + (notifyyear);
                        notifyDateStringfromCalendar = notifyDate;

                    }

                }, notifyyear, notifymonth, notifydayOfMonth);

Now in setOnCancelListener for the datepicker dialog, I want to have some code to hide the timepickerdialog.

        notifydatePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                String notifyDate;

                notifyDate = notifydayOfMonth + "/" + (notifymonth + 1) + "/" + (notifyyear);
                notifyDateStringfromCalendar = notifyDate;

            }
        });

        TimePickerDialog notifytimePickerDialog = new TimePickerDialog(Editor_Activity.this,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int notifyhourOfDay, int notifyminute) {

                        String notifyTime;

                        notifyTime = notifyhourOfDay + "/" + notifyminute;
                        notifyTimeStringfromCalendar = notifyTime;    

                    }
                }, notifyhourOfDay, notifyminute, false);


        notifytimePickerDialog.show();


        //DatePicker Dialog shows with not title as well as minimum date set
        notifydatePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
        notifydatePickerDialog.setTitle("");
        notifydatePickerDialog.show();
    }

Solution

  • Actually, it was pretty simple to find the answer.

    I just has to move the "notifydatePickerDialog.setOnCancelListener" below the TimePicker Dialog and add "notifytimePickerDialog.hide();" to the function.

    Apparently the date picker dialog's cancel button could not read the timepicker dialog while above it.