0

I'm trying to pass an ArrayList of unknown class type that extend an abstract class, to another activity using Parcelable. Since its not possible to use Parcelable.CREATOR on the abstract class, there is an error when I try to create the ArrayList: in.readTypedList(AbstractChannel.CREATOR), see below:

public class TvNetwork implements Parcelable {
    public String name;
    public ArrayList<? extends AbstractChannel> mChannels;

    public TvNetwork(String name, ArrayList<? extends AbstractChannel> channels) {
        this.name = name;
        this.mChannels = channels;
    }

    protected TvNetwork(Parcel in) {
        name = in.readString();
        mChannels = in.readTypedList(AbstractChannel.CREATOR); // here is the error
    }

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

        @Override
        public TvNetwork[] newArray(int size) {
            return new TvNetwork[size];
        }
    };


    public ArrayList<? extends AbstractChannel> getChannels() {
        return mChannels;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeTypedList(mChannels);
    }

}

Writing seems to work but not reading. This obviously does not work either, but explains a bit more what I want to do:

in.readTypedList(mChannels, <? extends AbstractChannel>.class.getClassLoader());

Any ideas?

A.C
  • 423
  • 2
  • 4
  • 13
  • Which error are you getting ? – sarcode May 30 '20 at 09:45
  • Does this answer your question? [Abstract class as parcelable](https://stackoverflow.com/questions/22576709/abstract-class-as-parcelable) – kasptom May 30 '20 at 09:52
  • @kasptom: problem is that I'm using unknown class type, extends AbstractChannel> and from what I can read its not possible to use this with Parcelable. – A.C May 30 '20 at 17:34

0 Answers0