java – CalendarView.getDate() inconsistent behaviour?-ThrowExceptions

Exception or error:

For my app, I want the user to be able to pick a date, or change it, if already picked. When I test this on my Samsung phone (API level 23), it works as expected, but if I test it on an emulated Pixel 2 (API level 24), changing the time doesn’t work.

I’m trying to understand why CalendarView behaves inconsistent when tested on different devices for me. Is it due to an issue with my code (more likely), or an issue with the emulator (less likely)?

Relevant code:

public class RecurringPaymentStartTimePicker extends Activity {
    private long iDate;                                         
    private int iHour;
    private Boolean newRP;

    private CalendarView date;
    private NumberPicker hour;

    @Override
    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);

        setContentView(R.layout.recurring_payment_start_time_picker);

        //either the current date or the previously chosen date
        iDate = super.getIntent().getLongExtra(Init.RECURRING_PAYMENT_ID_CODE+"date", 0);    
        iHour = super.getIntent().getIntExtra(Init.RECURRING_PAYMENT_ID_CODE+"hour", 0);
        newRP = super.getIntent().getBooleanExtra(Init.RECURRING_PAYMENT_ID_CODE+"new", true);

        TextView fixedDate = findViewById(R.id.recurring_payment_start_time_picker_fixed_date);
        date = findViewById(R.id.recurring_payment_start_time_picker_date);
        hour = findViewById(R.id.recurring_payment_start_time_picker_clock);

        hour.setMaxValue(23);
        hour.setMinValue(0);

        date.setDate(iDate, true, true);
        hour.setValue(iHour);
        if (newRP) {
            fixedDate.setText(getResources().getString(R.string.first_date));
        } else {
            fixedDate.setText(getResources().getString(R.string.next_date));
        }

        Button confirm = findViewById(R.id.recurring_payment_start_time_picker_confirm);
        Button cancel = findViewById(R.id.recurring_payment_start_time_picker_back);

        cancel.setText(getResources().getString(R.string.back));
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(Activity.RESULT_CANCELED, prepareIntent());
                finish();
                overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right);
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(Activity.RESULT_OK, prepareIntent());
                finish();
                overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right);
            }
        });
    }

    public Intent prepareIntent() {
        Intent intent = new Intent();
        iDate = date.getDate();
        iHour = hour.getValue();

        intent.putExtra(Init.RECURRING_PAYMENT_ID_CODE+"date", iDate);
        intent.putExtra(Init.RECURRING_PAYMENT_ID_CODE+"hour", iHour);

        return intent;
    }

    @Override
    public void onBackPressed() {
        Button cancel = findViewById(R.id.recurring_payment_start_time_picker_back);
        cancel.performClick();
    }
}

To test the behaviour, I just clicked on a different date than the currently chosen one and then clicked on the confirm button. On my Samsung phone, the date is correctly updated. On the emulated Pixel 2 however, the date did not get updated.

Using the debugger on the Pixel 2 after clicking on a different date, when inspecting element date > mDelegate > mDayPickerView > mTempCalendar > time, it shows as the same date as the CalendarView has been set to initially.

Am I doing something wrong that leads to this inconsistent behaviour, or is it an issue with the emulator?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *