0

I added Parcelable implementation for my class and get this code:

data class Category (val name:String, var factors:ArrayList<String>):Parcelable {
    constructor(parcel: Parcel) : this(
            parcel.readString(),
            TODO("factors")) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(name)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Category> {
        override fun createFromParcel(parcel: Parcel): Category {
            return Category(parcel)
        }

        override fun newArray(size: Int): Array<Category?> {
            return arrayOfNulls(size)
        }
    }
}

As far as I understand I should write method for reading data from ArrayList. I've tried to use

parcel.readArrayList()

but can not understand, how to insert String as ClassLoader. Please help me to understand, how to make this object Parcelable.

2 Answers 2

1

Use the following within your Constructor:

parcel.createStringArrayList()

and within writeToParcel:

parcel.writeStringList(this.mTest)

Just a side node: If you're using Android Studio, just let it generate the Parcelable with the Parcelable code generator Plugin.

Sign up to request clarification or add additional context in comments.

Comments

0

Read Parcelable

    segments=new ArrayList<>();
    in.readTypedList(segments,Segments.CREATOR);
    Multi = in.readString();

And for writing

 dest.writeTypedList(segments);
 dest.writeString(Multi);

And it works like a charm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.