Kotlinx Parcelize类扩展

时间:2018-08-17 07:45:38

标签: android kotlin parcelable kotlin-android-extensions

我有两个开放类BaseNfcCardSomeObjectNfcCard。 这里SomeObjectNfcCard扩展了BaseNfcCardParcelable,还有BaseNfcCard扩展了Parcelable

当我将SomeObjectNfcCard传递给另一个活动时,我仅从中获得数据,而没有BaseNfcCard的数据。这是我的代码问题还是lib?

@Parcelize
open class BaseCard(
        var cardId: String = "",
        var id: String = "",
        var type: Int = NfcFieldsHelper.UNDEFINED
) : Parcelable

@Parcelize
open class ObjectCard(
        var date: String = "",
        var objectState: Int = STATE_UNDEFINED,
        var managerId: String = "",
        var lastUpdate: String = "",

        var plombs: ArrayList<String> = ArrayList()
) : BaseCard(), Parcelable

将数据传递给意图

private fun onCardParsed(result: ObjectCard) = startActivity(Intent(this,
        CardReadyActivity::class.java)
        .putExtra(CardReadyActivity.CARD_DATA, result))

从意图中获取数据

private lateinit var card: ObjectCard
card = intent?.extras?.getParcelable(CardReadyActivity.CARD_DATA)!!

1 个答案:

答案 0 :(得分:0)

这实际上已在Parcelize中记录:

  

请注意,只有主要的构造方法属性会被序列化。

https://github.com/JetBrains/kotlin/blob/master/plugins/android-extensions/android-extensions-runtime/src/kotlinx/android/parcel/Parcelize.kt#L24

但是从他们的test cases来看,撰写应该可以:

@Parcelize
class ObjectCard(
        var baseCard: BaseCard,
        var date: String = "",
        var objectState: Int = 2,
        var managerId: String = "",
        var lastUpdate: String = "",
        var plombs: ArrayList<String> = ArrayList()
)