3

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.

You Qi
  • 8,353
  • 8
  • 50
  • 68
  • 1
    Meet the same problem. My way is remove the function and re-generate by the android studio. – Ninja Aug 26 '16 at 06:11
  • Just Remove all your previous Parceable implementation. And again extend ur class with "implements Parceable". ALT + ENTER the implementation and its regenerated including the new variables you added. – D Agrawal Aug 26 '16 at 06:15
  • @DAgrawal yes, that would be ideal if the developer remembers to do this every time. Question is what if he misses out? – You Qi Aug 26 '16 at 06:25
  • @YouQi : Then you miss out that variable and that wont be parceable , cant be careless about it, The moment you add a new variable, do as above. – D Agrawal Aug 26 '16 at 06:32
  • hence my question. the process used to be a single line change back in the serialization day. but now with Parcelable, every time I introduce a new field, I need to go thru the class to remove the `static Creator`, the `describerContent` method, the `writeToParcel`, and the `Constructor with Parcel argument` just so I can re-implement the whole thing by android studio? is there any better way in doing this or this is the only way for now? – You Qi Aug 26 '16 at 06:41
  • Only way for now. – D Agrawal Aug 29 '16 at 10:13

3 Answers3

3

Place your cursor on your class name which implements Parcelable interface and press alt+enter(option+enter in mac) meant for Projet quick fix. Click on the option called Replace Parcelable Implementation.This does the work for you.

Ramesh R
  • 329
  • 1
  • 6
0

You can create and update a parcelable class easily with NO HARD CODING approach and Android studio 4.1.2 can do it for you just in a few seconds !!!

Update an existing parcelable class:

Step 1: If you need to add some new field in your existing class then you need to remove the previous writeToParcel and describeContents methods and CREATOR class.

Step 2: Implement parcelable methods by pressing Alt+Enter for windows and Option+Enter for mac but you will find that the writeToParcel is empty for the first time -

enter image description here

Step 3: Then you will find that your class name is red now. So you need to add the CREATOR class as well as to fill the empty writeToParcel by pressing Alt+Enter for windows and Option+Enter for mac -

enter image description here

Finally, you will see your class will be a fully parcelable class now !!! To get more insight please see my real answer.

Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
0

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

You Qi
  • 8,353
  • 8
  • 50
  • 68