Kotlin-Parcelable遇到的坑,Parcelize使用

原创 Jzd_dev   2019-07-03 00:59  阅读 128 次

我们在页面间跳转时,经常需要传递值,在使用Java中我们实现Parcelable接口,实现响应方法即可。然而我在使用Kotlin的时候,遇到了点问题,问题如下:

问题
1.创建2个Activity,分别是MainActivity和Main2Activity,点击MainActivity上的Button,跳转到Main2Activity,并且传递我们的Bean。
2.创建MainActivity
MainActivity中跳转:

btn.setOnClickListener({
val intent = Intent(this, Main2Activity::class.java)
val user = User("jzd", 100)
intent.putExtra("user", user)

val bean = DispatchRecordBean("id", "plateNumber", "createTime", "reason", "startTime",
"endTime", "recipient", "model", "carColor", "carBrands", "carCategory", "carYear", 10)
intent.putExtra("bean", bean)

startActivity(intent)
})

User对象:

data class User(val name: String, val age: Int) : Parcelable
{
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readInt())
{
}

override fun writeToParcel(parcel: Parcel, flags: Int)
{
parcel.writeString(name)
parcel.writeInt(age)
}

override fun describeContents(): Int
{
return 0
}

companion object CREATOR : Parcelable.Creator<User>
{
override fun createFromParcel(parcel: Parcel): User
{
return User(parcel)
}

override fun newArray(size: Int): Array<User?>
{
return arrayOfNulls(size)
}
}
}

DispatchRecordBean对象:

data class DispatchRecordBean(var id: String, var plateNumber: String, var createTime: String, var reason: String,
var startTime: String, var endTime: String, var recipient: String, var carModel: String, var carColor: String, var carBrands: String, var carCategory: String,
var carYear: String, var dispatchType: Int) : Parcelable
{
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readInt())

override fun writeToParcel(parcel: Parcel, flags: Int)
{
parcel.writeString(id)
parcel.writeString(plateNumber)
parcel.writeString(createTime)
parcel.writeString(reason)
parcel.writeString(startTime)
parcel.writeString(endTime)
parcel.writeString(recipient)
parcel.writeString(carModel)
parcel.writeString(carColor)
parcel.writeString(carBrands)
parcel.writeString(carCategory)
parcel.writeString(carYear)
parcel.writeInt(dispatchType)
}

override fun describeContents(): Int
{
return 0
}

companion object CREATOR : Parcelable.Creator<DispatchRecordBean>
{
override fun createFromParcel(parcel: Parcel): DispatchRecordBean
{
return DispatchRecordBean(parcel)
}

override fun newArray(size: Int): Array<DispatchRecordBean?>
{
return arrayOfNulls(size)
}
}
}

3.Main2Activity中接收:

val user = intent.getParcelableExtra<User>("user")
mTvName.text = user?.name
mTvAge.text = user?.age.toString()

val bean = intent.getParcelableExtra<DispatchRecordBean>("bean")
mTvId.text = bean?.dispatchType.toString()

4.问题就是,我的User对象可以正常传值,而我的Dispatch对象就不能正常传递,Debug看一下代码,发现在Dispatch类的constructor方法中,parcel.readString()取值是跳跃式的取值,正常应该是按照id->plateNumber->createTime->reason->startTime->endTime->recipient->carModel->carColor->carBrands->carCategory->carYear->dispatchType,而Debug发现,parcel.readString()取到的值是id->createTime->startTime->recipient..这样的,到了后面字段值都取不到了,难道是一次取了2个值?在修改Dispatch代码时,发现取值情况还会变化,真的是一头雾水…到现在也不知道是什么情况,最好换成了Parcelize方式传值,解决了问题。
那么接下来说一下Parcelize怎么传递值:

Parcelize传值
在Module的build.gradle中添加:

apply plugin: 'kotlin-android-extensions'
android
{
androidExtensions {
experimental = true
}
}

修改Dispatch代码,添加Parcelize注解,最后Dispatch代码如下:

@SuppressLint("ParcelCreator")
@Parcelize
data class DispatchRecordBean(var id: String, var plateNumber: String, var createTime: String, var reason: String,
var startTime: String, var endTime: String, var recipient: String, var carModel: String, var carColor: String, var carBrands: String, var carCategory: String,
var carYear: String, var dispatchType: Int) : Parcelable

问题迎刃而解,暂时还是使用Parcelize注解传递值吧

本文地址:https://www.dandroid.cn/?p=6181
关注我们:请关注一下我们的今日头条号:扫描二维码大安卓源码网的头条号,头条号:大安卓源码
版权声明:本文为原创文章,版权归 Jzd_dev  所有,欢迎分享本文,转载请保留出处!

发表评论


表情