Android Kotlin creates an implementation of the Parcelable class gives an error in "overriding" the writeToParcel method

To use Parcelable, I followed this Kotlin 1.1.4 release: https://blog.jetbrains.com/kotlin/2017/08/kotlin-1-1-4-is-out/

Add this line to the project

androidExtensions { experimental = true } 

Then define the class:

 @Parcelize class User(val firstName: String, val lastName: String) : Parcelable 

The writeToParcel () and createFromParcel () methods are created automatically

 override fun writeToParcel(parcel: Parcel, flags: Int) { ... } 

but there is still an error in the 'override' key with the message

OVERRIDING_WRITE_TO_PARCEL_IS_NOT_ALLOWED: Overriding 'writeToParcel' is not allowed. Use the helper object "Parceler" instead

Can you show me the right way?

Edit: Will only the properties defined in the default constructor be added to Parcel, but not others? I see this warning in this class.

PROPERTY_WONT_BE_SERIALIZED: The property will not be serialized to 'Package'. Add @Transient annotation to remove warning

+7
android kotlin parcelable
source share
2 answers

Make sure you are using kotlin version 1.1.4

No need to override writeToParcel / createFromParcel methods. If you do nothing. The studio gives you an error, but you can ignore this error; still not updated to understand @Parcelize. The relevant YouTrack issue is here:

https://youtrack.jetbrains.com/issue/KT-19300

To use the create class

enter image description here

Then pass this as

enter image description here

To return it

enter image description here

+3
source share

You can simply ignore the warning as it only checks the lint.
now to get rid of it just use @SuppressLint("ParcelCreator")

Example:

 @SuppressLint("ParcelCreator") @Parcelize class User(val firstName: String, val lastName: String) : Parcelable 
+4
source share

All Articles