SoFunction
Updated on 2025-03-11

Detailed explanation and difference between Android Parcelable and Serializable

The difference between Android Parcelable and Serializable

1. Function

The function of Serializable is to save the properties of the object to local files, databases, network streams, and rmis to facilitate data transmission. Of course, this transmission can be within the program or between two programs. The original intention of Android's Parcelable is that Serializable is too slow and is designed to efficiently transmit data between different components in the program and between different Android programs (AIDL). These data only exist in memory. Parcelable is the carrier of messages communicated through IBinder.

From the above design we can see the pros and cons.

2. Efficiency and choice

Parcelable's performance is better than Serializable and has a smaller memory overhead. Therefore, when transferring data between memory, Parcelable is recommended for data transmission, such as transferring data between activities. Serializable can persist data and save it easily. Therefore, when you need to save or transfer data on the network, select Serializable when you need to save or transfer data on the network. Because different versions of Parcelable may be different, it is not recommended to use Parcelable for data persistence.

3. Programming implementation

For Serializable, the class only needs to implement the Serializable interface and provide a serialized version id (serialVersionUID). Parcelable needs to implement writeToParcel, describeContents functions and static CREATOR variables. In fact, it defines the work of how to package and unpack it yourself, and the serialization operations are completely implemented by the underlying layer.

An example of implementation of Parcelable is as follows

public class MyParcelable implements Parcelable { 
   private int mData; 
   private String mStr; 
 
   public int describeContents() { 
     return 0; 
   } 
 
   // Write data for saving   public void writeToParcel(Parcel out, int flags) { 
     (mData); 
     (mStr); 
   } 
 
   // Used to create custom Parcelable objects   public static final <MyParcelable> CREATOR 
       = new <MyParcelable>() { 
     public MyParcelable createFromParcel(Parcel in) { 
       return new MyParcelable(in); 
     } 
 
     public MyParcelable[] newArray(int size) { 
       return new MyParcelable[size]; 
     } 
   }; 
    
   // Read data for recovery   private MyParcelable(Parcel in) { 
     mData = (); 
     mStr = (); 
   } 
 } 

From the above we can see that the order of writing and reading of Parcel is consistent. If the element is a list, you need to pass it in first. Otherwise, a null pointer exception will be reported. as follows:

list = new ArrayList<String>();
(list);

PS: When using it yourself, when reading the data, the previous int data is mistakenly read out as long. The order afterwards is inconsistent, and the following exception is reported. When there are many class fields, be sure to keep the types and order of write and read consistent.

11-21 20:14:10.317: E/AndroidRuntime(21114): Caused by: : Parcel @4126ed60: Unmarshalling unknown type code 3014773 at offset 164

4. Advanced features

Serializable serialization does not save static variables. You can use the Transient keyword to not serialize some fields, or you can override writeObject and readObject methods to implement customization of the serialization process.

other:

: ClassNotFoundException when unmarshalling

refer to:

/reference/android/os/

Thank you for reading, I hope it can help you. Thank you for your support for this site!