为了使用Parcelable,我遵循了Kotlin 1.1.4:https://blog.jetbrains.com/kotlin/2017/08/kotlin-1-1-4-is-out/的这个版本

Add this line in project

androidExtensions {
    experimental = true
}

Then define a class :

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

writeToParcel()和createFromParcel()方法是自动创建的

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

but still have an error in 'override' keyword with message

OVERRIDING_WRITE_TO_PARCEL_IS_NOT_ALLOWED: Overriding 'writeToParcel' is not allowed. Use 'Parceler' companion object instead

Can you show me the right way?

Edit :

PROPERTY_WONT_BE_SERIALIZED: Property would not be serialized into a 'Parcel'. Add '@Transient' annotation to remove the warning

推荐答案

确保您使用的是kotlin 1.1.4版本

无需重写writeToParcel/createFromParcel方法.

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

使用create类

enter image description here

然后像

enter image description here

To get it back

enter image description here

Kotlin相关问答推荐

在intellij中使用kotlin符号和floordiv

我如何测试一个可组合组件没有显示,但如果它不存在也接受?

垂直滚动条下拉菜单的桌面组合

为什么 Kotlin main 函数需要 @JVMStatic 注解?

使用 Jetpack Compose 使用参数导航

将 java Optional 转换为 Kotlin Arrow Option

类型不匹配:Required: Context, Found: Intent

使用纯 Kotlin 函数作为 Junit5 方法源

如何使用 Android CameraX 自动对焦

是否可以在 kotlin 中嵌套数据类?

Kotlin 的 isNullOrBlank() 函数是否可以导入 xml 以用于数据绑定

Kotlin DataBinding 将静态函数传递到布局 xml

Kotlin协程处理错误和实现

从片段(fragment)中的点击事件启动协同程序

Kotlin中的Memoization功能

安装 Kotlin-Jupyter:e: java.lang.NoClassDefFoundError: 无法初始化类 org.jetbrains.kotlin.com.intellij.pom.java.LanguageLevel

Failure delivering result on activity result

Kotlin 中的填充字符串

Dagger 2 androidx fragment不兼容类型

Kotlin:如何使用扩展函数扩展枚举类