Stasky
Stasky

Reputation: 117

How to pass a Parcelable class which contains a list of another parcelable class?

I'm trying to pass a Parcelable class from one Activity to another. I do it like this:

Intent intent = new Intent(ClosedChatActivity.this, AdminProfileActivity.class);intent.putExtra("adminProfile", adminProfile);
startActivity(intent);

And then get it in the other Activity like this:

adminProfile = (AdminProfile) getIntent().getExtras().getParcelable("adminProfile");

This is the AdminProfile class and the WebLink class it has inside:

public class AdminProfile implements Parcelable {
    public static final Creator<AdminProfile> CREATOR = new Creator<AdminProfile>() {
        @Override
        public AdminProfile createFromParcel(Parcel in) {
            return new AdminProfile(in);
        }

        @Override
        public AdminProfile[] newArray(int size) {
            return new AdminProfile[size];
        }
    };
    public Long idUser;
    public String name;
    public String professio;
    public String description;
    public List<WebLink> webLinks;
    public Long idOficina;

    protected AdminProfile(Parcel in) {
        idUser = in.readLong();
        name = in.readString();
        professio = in.readString();
        description = in.readString();
        webLinks = in.createTypedArrayList(WebLink.CREATOR);

        idOficina = in.readLong();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeLong(idUser);
        parcel.writeString(name);
        parcel.writeString(professio);
        parcel.writeString(description);
        parcel.writeLong(idOficina);
        parcel.writeTypedList(webLinks);
    }
}
public class WebLink implements Parcelable {
    public static final Creator<WebLink> CREATOR = new Creator<WebLink>() {
        @Override
        public WebLink createFromParcel(Parcel in) {
            return new WebLink(in);
        }

        @Override
        public WebLink[] newArray(int size) {
            return new WebLink[size];
        }
    };
    public String name;
    public String url;

    protected WebLink(Parcel in) {
        name = in.readString();
        url = in.readString();
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        WebLink webLink = (WebLink) obj;
        assert webLink != null;
        return this.name.equals(webLink.name) && this.url.equals(webLink.url);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(name);
        parcel.writeString(url);
    }
}

When I get the object, the two Strings in it aren't the same I sent. Why is that?

The weird thing is that I have another Parcelable class which contains an AdminProfile, and when sending that class it does send fine with the Weblinks included, but if I send only an AdminProfile somewhere in the way it fails.

Upvotes: 0

Views: 160

Answers (1)

David Wasser
David Wasser

Reputation: 95578

When you read and write Parcelable classes, you need to make sure that the order of the elements is exactly the same.

You are writing idOficina followed by WebLinks, but you are reading them in the opposite order.

Upvotes: 1

Related Questions