But Serializing operations consume much performance. How can overcome
this?
So better use is to convert date into Long while writing, and read Long and pass to Date constructor to get Date. See below code
@Override
public void writeToParcel(Parcel out, int flags) {
// Write long value of Date
out.writeLong(date_object.getTime());
}
private void readFromParcel(Parcel in) {
// Read Long value and convert to date
date_object = new Date(in.readLong());
}
In Kotlin we may create extension for Parcel - the simplest solution.
fun Parcel.writeDate(date: Date?) {
writeLong(date?.time ?: -1)
}
fun Parcel.readDate(): Date? {
val long = readLong()
return if (long != -1L) Date(long) else null
}