In Android we know that we cannot just pass objects to activities. The objects must be either implements Serializable or Parcelable interface to do this.
Serializable
Serializable is a standard Java interface. You can just implement Serializable interface and add override methods.The problem with this approach is that reflection is used and it is a slow process. This method create a lot of temporary objects and cause quite a bit of garbage collection. Serializable interface is easier to implement.
Look at the example below (Serializable)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
//MyObjects Serializable class import java.io.Serializable; import java.util.ArrayList; import java.util.TreeMap; import android.os.Parcel; import android.os.Parcelable; public class MyObjects implements Serializable { private String name; private int age; public ArrayList address; public MyObjects(String name, int age, ArrayList address) { super(); this.name = name; this.age = age; this.address = address; } public ArrayList getAddress() { if (!(address == null)) return address; else return new ArrayList(); } public String getName() { return name; } public String getAge() { return age; } } //MyObjects instance MyObjects mObjects = new MyObjects("name","age","Address array here"); //Passing MyObjects instance via intent Intent mIntent = new Intent(FromActivity.this, ToActivity.class); mIntent.putExtra("UniqueKey", mObjects); startActivity(mIntent); //Getting MyObjects instance Intent mIntent = getIntent(); MyObjects workorder = (MyObjects) mIntent.getSerializableExtra("UniqueKey"); |
Parcelable
Parcelable process is much faster than serializable. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.
Look at the example below (Parcelable)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
//MyObjects Parcelable class import java.util.ArrayList; import android.os.Parcel; import android.os.Parcelable; public class MyObjects implements Parcelable { private int age; private String name; private ArrayList address; public MyObjects(String name, int age, ArrayList address) { this.name = name; this.age = age; this.address = address; } public MyObjects(Parcel source) { age = source.readInt(); name = source.readString(); address = source.createStringArrayList(); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(age); dest.writeString(name); dest.writeStringList(address); } public int getAge() { return age; } public String getName() { return name; } public ArrayList getAddress() { if (!(address == null)) return address; else return new ArrayList(); } public static final Creator CREATOR = new Creator() { @Override public MyObjects[] newArray(int size) { return new MyObjects[size]; } @Override public MyObjects createFromParcel(Parcel source) { return new MyObjects(source); } }; } MyObjects mObjects = new MyObjects("name","age","Address array here"); //Passing MyOjects instance Intent mIntent = new Intent(FromActivity.this, ToActivity.class); mIntent.putExtra("UniqueKey", mObjects); startActivity(mIntent); //Getting MyObjects instance Intent mIntent = getIntent(); MyObjects workorder = (MyObjects) mIntent.getParcelable("UniqueKey"); //You can pass Arraylist of Parceble obect as below //Array of MyObjects ArrayList mUsers; //Passing MyOjects instance Intent mIntent = new Intent(FromActivity.this, ToActivity.class); mIntent.putParcelableArrayListExtra("UniqueKey", mUsers); startActivity(mIntent); //Getting MyObjects instance Intent mIntent = getIntent(); ArrayList mUsers = mIntent.getParcelableArrayList("UniqueKey"); |
Conclusion.
- Parcelable is faster than serializable interface
- Parcelable interface takes more time for implemetation compared to serializable interface
- serializable interface is easier to implement
- serializable interface create a lot of temporary objects and cause quite a bit of garbage collection
- Parcelable array can be pass via Intent in android
— taken from sof