Serialization and Paracelable Object.
Parcelable is faster than Serialization makes it a perferred choice of approach while passing an object.
implementing the Parcelable interface and overriding the writeToParcel() method in its own class. and then to create a static Parcelable.Creator objcet to de-serialize the Java object.
Parcelable and Serilization are used for marshaling and unmarshaling java objects.
Parcelable is well documented in the Android SDK; Serialization on the other hand is avaliable in java.
Seralization is a marker interface, which implies the user cannot mashal the data. In Serialization, a mashaling operation is performed on a JVM using the java reflection API. This helps identify the java objcets member and behavior, but also ends up creating a lot of garbage objects. Due to this, the Serialization process is slow in comparsion to Parcelable.
Speed test in different mobile showing like this picture.
I will find some pictures in google when searching a unfamiliar words.
Parcelable
Interface for classes whose instances can be written to and restored from aParcel
. Classes implementing the Parcelable interface must also have a non-null static field called
CREATOR
of a type that implements the
Parcelable.Creator
interface.
Parcel
Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the generalParcelable
interface), and references to live
IBinder
objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.
Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable
API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.
The bulk of the Parcel API revolves around reading and writing data of various types. There are six major classes of such functions available.
writeByte(byte)
,
readByte()
,
writeDouble(double)
,
readDouble()
,
writeFloat(float)
,
readFloat()
,
writeInt(int)
,
readInt()
,
writeLong(long)
,
readLong()
,
writeString(String)
,
readString()
.
if u want copy list of Objcet, here is the code.
by the way, if one field not add in writeToParcel() when transfer data rely on Bundle. the other side will get a null object. Such as Activity A transfer data to Activity B.