0

You can see from the code below, parcel don't read any key for return the value. How do they know what to read and put into variable someString1 or someString2 and so on?

public SomeObject(Parcel in) {

   this.someString1 = in.readString();
   this.someString2 = in.readString();
   this.someInt = in.readInt();
   this.someString3 = in.readString();
   this.someInt2 = in.readInt();

}

What if I put it this way. Will the following code become error, because I change the order to read it?

public SomeObject(Parcel in) {

this.someString3 = in.readString();
this.someInt2 = in.readInt();
this.someString1 = in.readString();
this.someString2 = in.readString();
this.someInt = in.readInt();

}
stackex
  • 3,205
  • 1
  • 11
  • 16

1 Answers1

2

Yes, changing the order will definitely cause issues. Values are written sequentially, so whatever order you write the fields out to in writeToParcel(), you must read them back in in the same order when reading from the Parcel.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274