DialogFragment timepicker onCancel and onDismiss problem

Patryk Lipka :

I've tried to implement a Time Picker which is opened up by a SwitchCompat, so when I enable the option, the Time Picker shows up, it works fine but if I press cancel, or back button or simply press outside of the time picker, the Switch stays at the ON position and I want it to get back to OFF when I don't choose the time in Time Picker. Thought that maybe onCancel method could help but it does not, or simply I made some mistake with implementing it, there's the related code:

public class Settings extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, TimePickerDialog.OnDismissListener, TimePickerDialog.OnCancelListener {
    private EditText textLastHour;
    private KeyListener keyListener;
    private SwitchCompat hourSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        textLastHour = findViewById(R.id.finalHourText);
        textLastHour.setKeyListener(null);
        textLastHour.setHint("Option not selected");

        hourSwitch = findViewById(R.id.switch_hour);

        hourSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    DialogFragment timePicker = new TimePickerFragment();
                    timePicker.show(getSupportFragmentManager(), "time picker");

                } else {
                    textLastHour.setText("");
                }
            }
        });
    }


    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        textLastHour = findViewById(R.id.finalHourText);
        textLastHour.setText("Final alarm at: " + hourOfDay + ":" + minute);
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        hourSwitch.setChecked(false);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        hourSwitch.setChecked(false);
    }
}

I've tried both onDismiss and onCancel and neither have worked here, is there any solution to make the Switch go back to the original position if you cancel the Time Picker? Added the DatePicker to tags as I feel like it may have the same solution as TimePicker for that problem.

My TimePickerFragment Class:

public class TimePickerFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, DateFormat.is24HourFormat(getActivity()));
    }
}
DemoDemo :

It's actually simpler than I thought all you need to do is to add a dismiss listener and change your ui/logic accordingly

TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

    }
}, 10, 10, true);

dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        Toast.makeText(MainActivity.this, "dismissed", Toast.LENGTH_LONG).show();
    }
});

dialog.show();

You can use the standard TimePickerDialog

Edit: Put everything to onCreate() except for dialog.show(), dialog.show() goes into the ifChecked in the Switch

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27094&siteId=1