Serializable vs Parcelable in Android

When trying to pass the data between the different components in Android, we would often need to pass the whole objects. There are two options to do that, we could either implement standard Java interface called Serializable, or Parcelable – specifically designed for Android.

The latter is recommended in Android applications, because it turns object’s data fields into primitive data types first, so that performance is increased significantly – which is considerably noticeable when passing a lot of objects.

Serializable #

Implementing Serializable is a lot easier than Parcelable, we just need to implement it in the class that we want to pass as data. Also, we have to implement it in nested classes, if any. No additional code.

import java.io.Serializable;
import java.util.List;

public class CustomClass implements Serializable {
    int a, b;
    String s;
    List list;

    static class ListItem implements Serializable {
        int aa, bb;
        String ss;
    }
}

That’s it, no additional work. Now let’s create a sample Intent to send the data from one activity to another. We just add the object we want to send as an extra.

Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("CustomClass object", new CustomClass());
startActivity(intent);

We receive the object in other activity with getSerializableExtra():

CustomClass customClass = (CustomClass) getIntent()
        .getSerializableExtra("CustomClass object");

Parcelable #

Implementing Parcelable takes a little bit more effort, but you should take time to implement it because it’s faster and designed for Android. We also have to implement a static field called CREATOR, which is an object implementing the Parcelable.Creator<> interface.

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.List;

public class CustomClass implements Parcelable {
    int a, b;
    String s;
    List list;

    /* Constructor that will get object from parcel. */
    public CustomClass(Parcel in) {
        this.a = in.readInt();
        this.b = in.readInt();
        this.list = new ArrayList();
        in.readTypedList(list, ListItem.CREATOR);
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(a);
        parcel.writeInt(b);
        parcel.writeTypedList(list);
    }

    /* Interface that generates instances of Parcelable class. */
    static final Creator CREATOR = new Creator() {
        @Override
        public CustomClass createFromParcel(Parcel parcel) {
            return new CustomClass(parcel);
        }

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

    /* Nested classes also have to be Parcelable. */
    static class ListItem implements Parcelable {
        int aa, bb;
        String ss;

        public ListItem(Parcel in) {
            this.aa = in.readInt();
            this.bb = in.readInt();
            this.ss = in.readString();
        }

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

        @Override
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeInt(aa);
            parcel.writeInt(bb);
            parcel.writeString(ss);
        }

        static final Creator CREATOR = new Creator() {
            @Override
            public ListItem createFromParcel(Parcel parcel) {
                return new ListItem(parcel);
            }

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

Communication is done the same way as with Serializable, except using getSerializableExtra() we use getParcelableExtra().

Note: CustomClass was just made for presentational purposes, without contructors and methods, so have that in mind if trying to implement it in your own classes.

 
11
Kudos
 
11
Kudos