Search code examples
androidparcelable

Safe/Easy way to update Parcelable


Is there a common practice that can prevent missout fields in writeToParcel and Parcel contructor when introducing new member variables to a class that already implemented Parcelable?

The reason is because currently I have hundreds of pojo classes that is constantly changing (adding/renaming fields) during the development phase. I find it tedious to update Parceable every time I make changes, especially new developers who joined the team tends to miss out this change.

The legacy java serialization seems to have this well taken care.


Solution

  • Revisiting this in 2021. We can simply use the Parcelize annotation:

    import android.os.Parcelable
    import kotlinx.parcelize.Parcelize
    
    @Parcelize
    data class Headlines(
        val title: String,
        val description: String,
    ): Parcelable
    
    

    Further reading: https://developer.android.com/kotlin/parcelize