Bak Itzik
Bak Itzik

Reputation: 486

Parcel/un-parcel list of instances from base generic class

I have a base generic abstract class, lets call it Base<T> class.

This class extended by few subclass, with different types for T mData. Let's say that I have class Sub1 extends Base<byte []> and class Sub2 extends Base<Object>.

In addition, I have a instancesList with type of List<Base<T>>, compounded from instances of Sub1 and Sub2.

  1. How can I parcel/un-parcel this list as a whole?
  2. Where should I implement the Parcelable interface? Where does the CREATOR goes, if any?

Upvotes: 0

Views: 31

Answers (1)

David Wasser
David Wasser

Reputation: 95626

Normally you implement the reader (CREATOR) and the writer in the concrete classes, not in the abstract class.

When an instance of a concrete class is written to the Parcel, the name of the concrete class is also automatically written to the Parcel. When this is unparceled, the name of the concrete class is read, and the appropriate CREATOR is called to create a new instance of that concrete class.

There are some situations where you need to implement the reader and writer in the abstract class. In that case see Parcelable inheritance: abstract class - Which CREATOR? for more complex solutions.

Upvotes: 0

Related Questions