SoFunction
Updated on 2025-03-11

Android serialization implementation interface Serializable and Parcelable detailed explanation

1. Serializable (object)

Just implement the Serializable interface. Serialize an object into a state that can be stored or transferred, and then force it back to use (deserialize).

class Person : Serializable {
    var name = ""
    var age = 0
}
//transferOneActivity {
    val person = Person().apply {
        name = "Zhang San"
        age = 18
    }
    Intent(this, TwoActivity::).also {
        ("person", person)
        startActivity(it)
    }
}
//GetTwoActivity {
    val person = ("person") as Person    //Deserialize to object}

2. Parcelable (properties)

Decompose an object, and each attribute after decomposition is the data type supported by Intent.

2.1 Implementation method (not recommended)

Create a class to implement the Parcelable interface, define the properties, and press ctrl+1 to automatically generate the compiler for us.

  • Implement the Parcelable interface and override the two functions describeContents() and writeToParcel(). describeContents() just returns 0. writeToParcel() calls writeXXX() to write properties in the class one by one.
  • Create an associated object implementation interface named CREATOR, specify the generic type as <Person>, and override the two functions createFromParcel() and newArray(). Create a Person object in createFromParcel() and return it, and call readXXX() to read out the previously written properties one by one (note that the order of reading and writing must be consistent). Call arrayOfNulls() in newArray() and pass in the formal parameter size as the array size.
class Person() : Parcelable {
    var name = ""
    var age = 0
    constructor(parcel: Parcel) : this() {
        //Read        name = () ?: ""
        age = ()
    }
    //Write    override fun writeToParcel(parcel: Parcel, flags: Int) {
        (name)
        (age)
    }
    // Just return 0    override fun describeContents(): Int {
        return 0
    }
    //Accompaniment object implementation interface    companion object CREATOR : &lt;Person&gt; {
        //Return object        override fun createFromParcel(parcel: Parcel): Person {
            return Person(parcel)
        }
        //Return an array, the parameter size is the array size        override fun newArray(size: Int): Array&lt;Person?&gt; {
            return arrayOfNulls(size)
        }
    }
}
//The delivery method is the same as Serializable, and it is used as follows:TwoActivity {
    val person = ("person")
}

2.2 Annotation method (recommended)

Kotlin provides a simpler way to write, and all the passed properties need to be defined in the main construct.

//Module Gradle introduces plug-inplugins {
    id 'kotlin-parcelize'
}
//use@Parcelize
class Person(val name: String, var age: Int) : Parcelable

This is the article about the detailed explanation of Android serialization interface Serializable and Parcelable. For more related content on Android Serializable and Parcelable, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!