0

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?
    }

}
Zied R.
  • 4,964
  • 2
  • 36
  • 67
Reza Bigdeli
  • 1,142
  • 1
  • 12
  • 25
  • why do you need to use parcelable?? extend from serializable if all you want to do is pass data from one activity to another. Java will take care of marshalling and unmarshalling by itself. – ichthyocentaurs Apr 19 '15 at 10:01
  • 1
    @AkshatArora because Parcelable is MUCH faster – inmyth Apr 19 '15 at 10:06
  • Well, yes, but I doubt you would see a lot of difference with just 3 class variables, also a lot of complexity may be removed from your code. I would suggest use of parcelables more while using heavy model objects or for any kind of AIDL interactions. Anyways my answer is given below. – ichthyocentaurs Apr 19 '15 at 10:17
  • Of curse it's just an example... my entities are super complex... I need to make them parcelable... – Reza Bigdeli Apr 19 '15 at 10:22

3 Answers3

1

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

Leonid Veremchuk
  • 1,952
  • 15
  • 27
1

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.

inmyth
  • 8,880
  • 4
  • 47
  • 52
1

For a String[] you can use the API

parcel.writeStringArray(url)

For AnotherEntity you need to extend it with Parcelable again

parcel.writeParcelable();
ichthyocentaurs
  • 2,173
  • 21
  • 35