扣丁书屋

Android中Parcelable接口的Koltin实现方式

2年以前  |  阅读数:152 次  |    

data class UserBean(
        var name: String,
        var age: Int
) : Parcelable {
    constructor(source: Parcel) : this(source.readString(), source.readInt())
    override fun describeContents() = 0
    override fun writeToParcel(dest: Parcel?, flags: Int) {
        dest?.writeString(name)
        dest?.writeInt(age)
    }
    companion object {
        @JvmField val CREATOR: Parcelable.Creator<UserBean> = object : Parcelable.Creator<UserBean> {
            override fun createFromParcel(source: Parcel): UserBean = UserBean(source)
            override fun newArray(size: Int): Array<UserBean?> = arrayOfNulls(size)
        }
    }
}

相关文章: