You have (at least) two possibilities:
-
Make MyClass implement Serializable, which is not advisable for this effect (1) .
Date and BigDecimal implement Serializable, they will be processed without problem.
You should use getSerializableExtra()
instead of getParcelableExtra()
.
-
Change the MyClass so that Parcel stores a String
and a long
with the BigDecimal and Date values. They, after being recovered, are used to recreate them.
public class ClassParcelable implements Parcelable{
private Date someDate;
private BigDecimal someBigValue;
public ClassParcelable(Date someDate, BigDecimal someBigValue){
this.someDate = someDate;
this.someBigValue = someBigValue;
}
//Implementação da Parcelable
@Override
public void writeToParcel(Parcel dest, int flags) {
//Guarda um long que representa a data
dest.writeLong(someDate.getTime());
//Guarda uma String com a representação do valor do BigDecimal
dest.writeString(someBigValue.toPlainString());
}
protected ClassParcelable(Parcel in) {
//Recupera o long que representa a data e recria-a
someDate = new Date(in.readLong());
//Recupera a String que representa o valor do BigDecimal e recria-o
someBigValue = new BigDecimal(in.readString());
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<ClassParcelable> CREATOR = new Creator<ClassParcelable>() {
@Override
public ClassParcelable createFromParcel(Parcel in) {
return new ClassParcelable(in);
}
@Override
public ClassParcelable[] newArray(int size) {
return new ClassParcelable[size];
}
};
}
(1) - Reflection is used during the process and many additional objects are created, which can cause a lot of garbage collection. The result is poor performance and high battery consumption.