How To Get Date Extra From An Intent?
I am packing an intent and one of the Extras I add is a date object, like this: intent.putExtra(DATE_EXTRA, t.getDate()); Later, when I reading the extras, I try to get the Date l
Solution 1:
Replace:
this.date = new Date(intent.getExtras().getString(DATE_EXTRA));
with:
this.date = (Date)intent.getSerializableExtra(DATE_EXTRA);
and see if that helps.
Solution 2:
Another workaround would be to pass the long value of the date:
intent.putExtra(DATE_EXTRA, t.getDate().getTime());
....
this.date = newDate(intent.getLongExtra(DATE_EXTRA, 0)); //0 is the default value
Solution 3:
Safe way in kotlin:
intent.putExtra("MOVE_DATE", date)
and to read it in safe way by check for key & null check:
val moveDate: Date?
intent?.apply {
if (hasExtra("MOVE_DATE")) {
moveDate = getSerializableExtra("MOVE_DATE") as? Date
moveDate?.let { print(it) }
}
}
Post a Comment for "How To Get Date Extra From An Intent?"