Sobes.tech
Junior

What is serialization in the context of Android development?

sobes.tech AI

Answer from AI

Serialization is the process of converting an object into a stream of bytes for storage (e.g., in a file, database) or transmission over a network. Deserialization is the reverse process of restoring an object from a byte stream.

In Android, it is often used for:

  • Saving the state of an Activity/Fragment during configuration changes (using onSaveInstanceState).
  • Transferring data between Activity/Service/BroadcastReceiver via Intent (using putExtra).
  • Saving data in SharedPreferences or files.
  • Network data transmission.

Android provides two main serialization mechanisms:

  1. Serializable:

    • Standard Java interface.
    • Easy to implement (just implement the interface).
    • Can be slower and create more temporary objects compared to Parcelable.
    • Some classes (e.g., TextView) are not serializable.
    // Example of Serializable
    import java.io.Serializable;
    
    public class MySerializableObject implements Serializable {
        private String name;
        private int value;
    
        // Constructor, getters, setters...
    }
    
  2. Parcelable:

    • Android interface optimized for IPC (Inter-Process Communication).
    • More efficient and performant compared to Serializable in Android.
    • Requires more manual work to implement (writeToParcel, createFromParcel).
    • Preferred method for passing data between Android components.
    // Example of Parcelable
    import android.os.Parcel;
    import android.os.Parcelable;
    
    public class MyParcelableObject implements Parcelable {
        private String name;
        private int value;
    
        // Constructor
        public MyParcelableObject(String name, int value) {
            this.name = name;
            this.value = value;
        }
    
        protected MyParcelableObject(Parcel in) {
            name = in.readString();
            value = in.readInt();
        }
    
        public static final Creator<MyParcelableObject> CREATOR = new Creator<MyParcelableObject>() {
            @Override
            public MyParcelableObject createFromParcel(Parcel in) {
                return new MyParcelableObject(in);
            }
    
            @Override
            public MyParcelableObject[] newArray(int size) {
                return new MyParcelableObject[size];
            }
        };
    
        @Override
        public int describeContents() {
            return 0; // Usually 0
        }
    
        @Override
        public void writeToParcel(Parcel parcel, int flags) {
            parcel.writeString(name);
            parcel.writeInt(value);
        }
        // Getters, setters...
    }
    

Comparison:

Characteristic Serializable Parcelable
Interface java.io.Serializable android.os.Parcelable
Performance Slower Faster
Ease of implementation Easier Requires more code
Usage General Java, object saving Preferred for Android IPC
Size Higher overhead Lower overhead

In Android, Parcelable is often recommended for better performance. There are plugins and tools that simplify code generation for Parcelable.

What is serialization in the context of Android… - sobes.tech