Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Parcelable Issue

I had to pass a dataobject from one activity to another. The best way to do this is uing Parcelable. The dataobject had some fields with setter and getter methods. After setting some fields and passing the object to another activity, wha tI observed is that the field values got interchanged to other field values. The order of fields for writing to parcel and reading from parcel 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

like image 353
Harshul Pandav Avatar asked Dec 16 '22 01:12

Harshul Pandav


2 Answers

It is not the order but the data type that is not the same, from the first 4 lines you write int, string, string, int then you read int, string, string, long. I didn't check any further, you must match both order and datatype of read and write operations.

like image 157
MahdeTo Avatar answered Jan 05 '23 03:01

MahdeTo


You are writing int

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

But reading long

this.AppRcptDt = new Date(in.readLong());
like image 32
waqaslam Avatar answered Jan 05 '23 02:01

waqaslam