Reza Bigdeli
Reza Bigdeli

Reputation: 1172

Android Parcelable: reading and writing

I want to make a custom entity class Parcelable.. I have some fields in it: a String[] and another custom entity object (which is parcelable).. I want to know how to read and write these objects and lists..

public class CustomEntity implements Parcelable {
    private int number;
    private String[] urls;
    private AnotherEntity object;

    public CustomEntity(Parcel in) {
        number = in.readInt();
        // how should I read urls?
        // how should I read object?
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(number);
        // how should I write urls?
        // how should I write object?
    }

}

Upvotes: 0

Views: 1102

Answers (3)

ichthyocentaurs
ichthyocentaurs

Reputation: 2175

For a String[] you can use the API

parcel.writeStringArray(url)

For AnotherEntity you need to extend it with Parcelable again

parcel.writeParcelable();

Upvotes: 1

inmyth
inmyth

Reputation: 9070

I definitely think you should NOT handle the boilerplate yourself. There are libraries around like Parceler where with only one annotation on your POJO and one line like Parcel.wrap or Parcel.unwrap you can do instant serialization.

Upvotes: 1

Leonid Veremchuk
Leonid Veremchuk

Reputation: 1963

https://github.com/mcharmas/android-parcelable-intellij-plugin use this plugin! Your AnotherEntity must implemented Parcelable too!

Upvotes: 1

Related Questions