how to properly implement Parcelable with an ArrayList<Parcelable>? how to properly implement Parcelable with an ArrayList<Parcelable>? android android

how to properly implement Parcelable with an ArrayList<Parcelable>?


You almost got it !

You just need to do :

public void writeToParcel(Parcel out, int flags) {    out.writeString(_mac);    out.writeString(_pan);    out.writeInt(_band);    out.writeSerializable(_lqis);    out.writeTypedList(_devices);}private ZigBeeNetwork(Parcel in) {    _mac = in.readString();    _pan = in.readString();    _band = in.readInt();    _lqis = (ArrayList<Integer>) in.readSerializable();    in.readTypedList(_devices, ZigBeeDev.CREATOR);}

That's all!

For your list of Integer, you can also do :

out.writeList(_lqis);_lqis = new ArrayList<>();in.readList(_lqis Integer.class.getClassLoader());

It should work.


In my case in.readTypedList(_devices, ZigBeeDev.CREATOR); gave me a NullPointerException on _devices. So I used this:

_devices = in.createTypedArrayList(ZigBeeDev.CREATOR);


You should use writeList(List l) for your list of integers and writeTypedList(List val) for the list of ZigBeeDevices