0

I thought I understood how to read/write from a parcel, but now I'm getting stuck. In one of my parcelable objects, I have this function

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(7);
    int testInt = dest.readInt();
    Log.d("test","INT = " + testInt);
}

which gets run correctly when the state of the activity is saved. This outputs

INT = 0

which I assume is because I'm not correctly writing / reading to the parcel. Or maybe you're not allowed to write and read immediately. What am I doing wrong?

Noah Santacruz
  • 460
  • 1
  • 3
  • 18

1 Answers1

0

You're supposed to write to the Parcel in the writeToParcel method, and read from the Parcel in the Creator's createFromParcel method.

I'm not sure why you would want to read from a Parcel in a writeToParcel method, but if you do do that, you should not expect the most recently written int to be read first; the data is read in the same order it was written.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • Yeah, I understand it's completely useless to read and write in the writeToParcel function. I was just trying to test if I understood how it worked. But the fact that I got 0 seemed weird. I got 0 when I tried it in the createFromParcel function as well. So my question still isn't answered – Noah Santacruz Sep 28 '15 at 22:53
  • @Bob In that case you need to show us a complete runnable example. It's hard to answer without seeing the methods in full and information about when and how the object is written to and read from the parcel. – Paul Boddington Sep 28 '15 at 22:59