The bug solution for the OnDateSetListener of DatePickerDialog being called twice

The bug solution for the OnDateSetListener of DatePickerDialog being called twice

When using DatePickerDialog.OnDateSetListenerit, it was found that the callback was called twice. The reason seems to be a source code problem.

solution:

Determine if the view is displayed

final Context context = MainActivity.this;
                Calendar calendar = Calendar.getInstance();
                int y = calendar.get(Calendar.YEAR);
                int m = calendar.get(Calendar.MONTH);
                int d = calendar.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        if (view.isShown()) { // 这里判断
                            Toast.makeText(context, String.format(Locale.CHINA, "(y, m, d) = (%d, %d, %d)", year, month, dayOfMonth), Toast.LENGTH_SHORT).show();
                        }
                    }
                };
                DatePickerDialog datePickerDialog = new DatePickerDialog(context, onDateSetListener, y, m, d);
                datePickerDialog.show();

Reference article:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325567999&siteId=291194637