How to make a class with nested objects Parcelable

前端 未结 7 732
梦如初夏
梦如初夏 2020-12-04 12:08

I\'d like to make class A Parcelable.

public class A {
    public String str;
    public ArrayList list;
}

This is what I\'ve come

7条回答
  •  时光说笑
    2020-12-04 13:00

    At Write to parcel

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(name); //if String
        parcel.writeTypedList(assignedEmployees); //if List with custom class, eg. List assignedEmployees
        parcel.writeParcelable(checkin,i); //if custom class, eg. Checkin checkin;
        }
    

    At Constructor for reading it back

     protected A(Parcel in) {
            name = in.readString();
            assignedEmployees = in.createTypedArrayList(AssignedEmployee.CREATOR);
            checkin = in.readParcelable(Checkin.class.getClassLoader());
        }
    

    where AssignedEmployee, Checkin where custom classes and it should implement Parcelable.

提交回复
热议问题