0

I'm trying to put a Parcelable object as an extra in an intent and pass it to the next Activity, and it doesn't crash but the object changes dramatically. I'm sending when clicking on an item from a RecyclerView in a Fragment and opening an Activity from it.

This is how I send it:

AdminProfile adminProfile = list.get(position).admin;
Intent intent = new Intent(view.getContext(),ClosedChatActivity.class);
intent.putExtra("chat",adminProfile);
view.getContext().startActivity(intent);

This how I get it:

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

And here the class:

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) {
        if (in.readByte() == 0) {
            idUser = null;
        } else {
            idUser = in.readLong();
        }
        name = in.readString();
        professio = in.readString();
        description = in.readString();
        webLinks = in.createTypedArrayList(WebLink.CREATOR);
        if (in.readByte() == 0) {
            idOficina = null;
        } else {
            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.writeList(webLinks);
    }
}

I can't understand why, but when I send the object I have UserId=3, but when I get it it's userId=55834574848. Any ideas?

Stasky
  • 107
  • 1
  • 5

1 Answers1

0

The Parcelable functions were filled automatically by Android Studio, and reading the first byte messed it up.

Changing

if (in.readByte() == 0) {
    idUser = null;
} else {
    idUser = in.readLong();
}

for

idUser = in.readLong();

fixed it.

Stasky
  • 107
  • 1
  • 5