0

I have this simple model class here:

public class News implements Parcelable {
    private String title;
    private String content;

    public News(String title, String content) {
        this.title = title;
        this.content = content;
    }

    protected News(Parcel in) {
        title = in.readString();
        content = in.readString();
    }
}

Quite simple and works well. However, what I don't understand is, how does Parcel know what to read when I say readString()? Both the title and content are just Strings and I call the exact same method. How come Android doesn't mix up the results?

Or do I have to specify an order in which I call the in.readString()s?

I'd like someone skilled to clear things up for me.

agiro
  • 2,018
  • 2
  • 30
  • 62
  • 1
    Yes, order matters. You specify it in the order in which you write the values. – Mike M. Nov 18 '17 at 10:58
  • Thank you. I uploaded a simple model here but the one I use is waaay more complex, so you might just have saved me hours of debugging :D If you wish to make this an answer, please do so. The documentation says only that it reads a string, that's it. – agiro Nov 18 '17 at 10:59
  • 1
    see `Parcelable#writeToParcel` – pskink Nov 18 '17 at 10:59
  • I was just gonna close it as a duplicate, actually, since it's been addressed here before. Thanks, though. – Mike M. Nov 18 '17 at 11:03
  • Thanks for the heads up, after checking the question you mentioned, indeed it was asked before. Just my googling skills failed me :( By the way why did the asker got so many downvotes there? it's a completely valid question. – agiro Nov 18 '17 at 11:38
  • That user got, I think, one downvote to begin with, but then they opened a question on https://meta.stackoverflow.com that was basically a rant about downvotes, and they made the mistake of linking directly to that particular question. The meta effect happened, and they just kinda brought more downvotes on themself. – Mike M. Nov 18 '17 at 12:01
  • Makes sense. It would be beneficial if a downvote would require comments on what to improve IMO but yeah - ranting has never ever got anyone far. – agiro Nov 18 '17 at 12:08

0 Answers0