如何使用Kotlin @parcelize从包裹中创建?
我正在尝试将 Parcelable 模型从 Java 迁移到 Kotlin。
我的模型如下所示:
import kotlinx.parcelize.*
@Parcelize
class SignatureAuthorityModel
(var id: Int,
var cloudId: Int,
var uuid: UUID?): android.os.Parcelable
{
override fun equals(other: Any?): Boolean
{
//equals implementation
}
override fun hashCode(): Int
{
//some hash implementation
}
override fun toString(): String
{
//some stringifier implementation
}
}
我可以为此模型调用 writeToParcel,但 SignatureAuthorityModel.createFromParcel(parcel) 不可用。我必须单独编写它,还是必须以某种方式配置 kotlin-parcelize 插件?从kotlin文档看来,当我使用@Parcelize注释时,createFromParcel也应该自动生成,并且只有当我有一些高级逻辑时,我才必须在伴生对象Creator中编写实现。正确的做法是什么?
I'm trying to migrate parcelable model from Java to Kotlin.
My model looks like this:
import kotlinx.parcelize.*
@Parcelize
class SignatureAuthorityModel
(var id: Int,
var cloudId: Int,
var uuid: UUID?): android.os.Parcelable
{
override fun equals(other: Any?): Boolean
{
//equals implementation
}
override fun hashCode(): Int
{
//some hash implementation
}
override fun toString(): String
{
//some stringifier implementation
}
}
I can call writeToParcel for this model, but SignatureAuthorityModel.createFromParcel(parcel) isn't available. Must I write it separately, or must I configure kotlin-parcelize plugin somehow? From kotlin documentation it seems that createFromParcel should also be generated automatically when I use @Parcelize annotation, and that I must write implementation in companion object Creator only if I have some advanced logic. What is the proper way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用的是
@parcelize
注释,则无需编写createfromparcel
方法。因为@parcelize
管理createfromparcel
内部方法。只需按照以下内容写入您的数据类。可能是您错误地导入了
包裹
,并且您的班级应如下所示。If you are using
@Parcelize
annotation then you don't need to writecreateFromParcel
method. Because@Parcelize
managescreateFromParcel
method internally.just write your data class like below. And might be you wrong import for
Parcelable
and your class should be like below.似乎已知的问题是,使用@parcelize
查看5年前创建的问题是无法访问的: https://youtrack.jetbrains.com/issue/kt-19853
遵循评论后,似乎他们对此并没有做任何事情。
我个人的看法是,他们可能不会为此而打扰,因为对于最常见的用例,这不是必需的。在Android开发中最常见的用例是使用
intent.putextra()
或 intent.putparcelablearablearraylistextra() ,在这种情况下变成包裹和背部的变形自动发生。编辑:
嗯..实际上他们在这里提到 https://github.com/jetbrains 应该使用
ParcelableCreator
可以使用它It seems to be a known issue that the creator is not accessible when using @Parcelize
See this issue created 5 years ago: https://youtrack.jetbrains.com/issue/KT-19853
Following the comments there it doesn't seem like they are doing anything about it.
My personal opinion is that they probably don't bother about it because for the most common use case it's not necessary. The most common use case for Parcelable in Android development is to pass the objects from one Activity to another using
intent.putExtra()
orintent.putParcelableArrayListExtra()
and in that case the the turning into Parcel and back happens automatically.EDIT:
hmm.. actually they mention here https://github.com/JetBrains/kotlin/pull/4575 that it should be available using
parcelableCreator
but I can't figure out how to use it or if it's even actually in the latest version要检索由
@Parcelize
注解的Parcelable
类的CREATOR
,只需使用正确的类型信息调用parcelableCreator
即可。请参阅下面的示例:To retrieve the
CREATOR
ofParcelable
classes annotated by@Parcelize
, simply callparcelableCreator
with the correct type information. See below for an example:根据 此拉取请求,有一种方法可以访问
CREATOR
同时仍然使用@Parcelize
注释。我无法发现仅阅读 PR 或文档是如何做到的。然而,我确实(在 PR 中)找到了一个令人愉快的小功能,并且迄今为止已经取得了成功。我轻松地将其复制并粘贴到我的项目中。调用它将返回一个
CREATOR
,然后您可以从中调用createFromParcel(parcel)
。下面是一个简单测试中的用法示例,该测试先进行包裹,然后从包裹创建。
这是一个链接到我的确切位置找到了辅助内联函数。它可能并不适用于所有情况,但到目前为止它对我来说很好。
According to this pull request, there is a means of accessing the
CREATOR
while still using the@Parcelize
annotation. I couldn't discover how just reading the PR or docs alone.However, I did manage to find (in the PR) a pleasant little function that has so far been successful. I copied and pasted it easily into my project. Calling it will return a
CREATOR
from which you can then callcreateFromParcel(parcel)
.Here's an example of its usage in a simple test that parcels, then creates from parcel.
Here is a link to exactly where I found the helper inline function. It may not work in every case, but so far it's been good to me.