onDateSet isn't getting called in android 5.1.1

无人久伴 提交于 2020-04-18 05:46:32

问题


I have a date picker dialog which works perfectly on android 9 and android 10, I decided to test on android 5.1.1, and after selecting a date, nothing happens, the onDateSet() doesn't fire up, I've read at least 10 articles on SO that refer to similar problems, but can't find a working solution

Here's my code, maybe someone could point out what i'm doing wrong

DateDialog.java

public class DateDialog extends DialogFragment {
    DatePickerDialog.OnDateSetListener mOnDateSetListener;
    public static final String BUNDLE_CURRENT_DATE = "current_date";

    private long currentDateMillis = 0;

    public static DateDialog getInstance(long currentDate) {
        Bundle args = new Bundle();
        args.putLong(BUNDLE_CURRENT_DATE, currentDate);
        DateDialog dateDialog = new DateDialog();
        dateDialog.setArguments(args);
        return dateDialog;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mOnDateSetListener = (DatePickerDialog.OnDateSetListener) context;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mOnDateSetListener = null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        currentDateMillis = getArguments().getLong(BUNDLE_CURRENT_DATE);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar currentDateCalender = Calendar.getInstance();
        currentDateCalender.setTimeInMillis(currentDateMillis);
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), mOnDateSetListener, currentDateCalender.get(Calendar.YEAR)
                , currentDateCalender.get(Calendar.MONTH), currentDateCalender.get(Calendar.DAY_OF_MONTH));
        long maxDate = System.currentTimeMillis() + TimeUnit.DAYS.toMillis(60);
        datePickerDialog.getDatePicker().setMaxDate(maxDate);
        Calendar tomorrow = Calendar.getInstance();
        tomorrow.add(Calendar.DATE, 1);
        datePickerDialog.getDatePicker().setMinDate(tomorrow.getTimeInMillis());
        return datePickerDialog;
    }
}

And this is how I start the dialog in my activity

MyActivity.Java

public void editExpiryDate() {
        DateDialog dialog = DateDialog.getInstance(selectedExpiryDate);
        dialog.show(getFragmentManager(), "");
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        Calendar newDate = Calendar.getInstance();
        newDate.set(Calendar.MONTH, month);
        newDate.set(Calendar.YEAR, year);
        newDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        selectedExpiryDate = newDate.getTimeInMillis();

        String expiryDateDisplayed = dateFormat.format(new Date(selectedExpiryDate)) + " (" +
                DateUtil.getNumberOfDaysToGo(selectedExpiryDate) + " days)";
        expiryDateTextView.setText(expiryDateDisplayed);
    }

来源:https://stackoverflow.com/questions/61261413/ondateset-isnt-getting-called-in-android-5-1-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!