Android Parcelable Issue

I had to transfer a data object from one activity to another. The best way to do this is to use Parcelable. There were some fields in the Dataobject with the setter and getter methods. After setting some fields and transferring the object to another activity, I noticed that the field values ​​change to other field values. The order of the fields for writing to the package and reading from the package is the same.

public void writeToParcel(Parcel out, int flags) {
    out.writeInt(id);
    out.writeString(appNo);
    out.writeString(this.policyNo);
    out.writeInt((int)this.AppRcptDt.getTime());
    out.writeString(this.currentStatus);
    out.writeString(this.productCd);
    out.writeDouble(this.sumAssured);
    out.writeDouble(this.modalPremium);
    out.writeDouble(this.annualPremium);
    out.writeString(this.paymentMode);
    out.writeString(this.branchCd);
    out.writeString(this.branchName);
    out.writeString(this.insuredName);
    out.writeString(this.auraStatus);
    out.writeString(this.ownerName);
    out.writeString(this.agentCd);
    out.writeString(this.billingMode);
}

private ApplicationTrackerDO(Parcel in) {
    id=in.readInt();
    this.appNo = in.readString();
    this.policyNo = in.readString();
    this.AppRcptDt = new Date(in.readLong());
    this.currentStatus = in.readString();
    this.productCd = in.readString();
    this.sumAssured = in.readDouble();
    this.modalPremium = in.readDouble();
    this.annualPremium = in.readDouble();
    this.paymentMode = in.readString();
    this.branchCd = in.readString();
    this.branchName = in.readString();
    this.insuredName = in.readString();
    this.auraStatus = in.readString();
    this.ownerName = in.readString();
    this.agentCd = in.readString();
    this.billingMode = in.readString();
}

difference in field values

+3
source share
4 answers

This is not an order, but a data type that is not the same, from the first 4 lines that you write int, string, string, int, then you read int, string, string, long. I didn’t check anymore, you have to match both the order and data type of the read and write operations.

+10

int

out.writeInt((int)this.AppRcptDt.getTime());

long

this.AppRcptDt = new Date(in.readLong());
+1

@Override
    public void writeToParcel ( Parcel dest, int flags ) {
        dest.writeInt((birthday != null) ? 1 : 0); // is birthday set?
        dest.writeLong((birthday != null) ? birthday.getTime() : 0);
    }

    public void readFromParcel ( Parcel in ) {
        if(in.readInt() == 1) {
            birthday = new Date(in.readLong());
        } else {
            in.readLong(); // ignore stored value
            birthday = null;
        }
    }
0
source

java.util.Date implements Serializable:

public class Date implements Serializable, Cloneable, Comparable<Date> {

    private static final long serialVersionUID = 7523967970034938905L;

    // Used by parse()
    private static final int CREATION_YEAR = new Date().getYear();

    private transient long milliseconds;


Using:

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeSerializable(myDateField);
}
private MyClass(Parcel in) {
    myDateField = (Date)in.readSerializable();
}
0
source

All Articles