Why does Android provide 2 interfaces for serializing objects? Do Serializable objects interopt with Android Binder
and AIDL files?
Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn't create more temp objects while passing the data between two activities.
A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.
Serializable is a standard Java interface. It is not a part of the Android SDK.
We can directly use parcelize to handle this with minimal code. This Parcelize annotation tells the Kotlin compiler to generate the writeToParcel() , and describeContents() methods, as well as a CREATOR factory class automatically. The class can be a data class but it's optional.
In Android we cannot just pass objects to activities. To do this the objects must either implement Serializable
or Parcelable
interface.
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 creates a lot of temporary objects and causes quite a bit of garbage collection. However, Serializable
interface is easier to implement.
Look at the example below (Serializable):
// 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<String> address; public MyObjects(String name, int age, ArrayList<String> address) { super(); this.name = name; this.age = age; this.address = address; } public ArrayList<String> getAddress() { if (!(address == null)) return address; else return new ArrayList<String>(); } public String getName() { return name; } // return age public int 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):
// 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<String> address; public MyObjects(String name, int age, ArrayList<String> 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<String> getAddress() { if (!(address == null)) return address; else return new ArrayList<String>(); } public static final Creator<MyObjects> CREATOR = new Creator<MyObjects>() { @Override public MyObjects[] newArray(int size) { return new MyObjects[size]; } @Override public MyObjects createFromParcel(Parcel source) { return new MyObjects(source); } }; }
// MyObjects instance 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.getParcelableExtra("UniqueKey");
You can pass ArrayList
of Parcelable objects as below:
// Array of MyObjects ArrayList<MyObjects> 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<MyObjects> mUsers = mIntent.getParcelableArrayList("UniqueKey");
Conclusion
Parcelable
is faster than Serializable
interfaceParcelable
interface takes more time to implement compared to Serializable
interfaceSerializable
interface is easier to implementSerializable
interface creates a lot of temporary objects and causes quite a bit of garbage collectionParcelable
array can be passed via Intent in androidSerializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations.
Parcelable is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient that Serializable, and to get around some problems with the default Java serialization scheme.
I believe that Binder and AIDL work with Parcelable objects.
However, you can use Serializable objects in Intents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With