android - 如何将 safe-args 与 MPP 可序列化数据一起使用

标签 android android-architecture-navigation kotlin-multiplatform android-safe-args

我正在尝试将 safe-args 与 kotlin-multiplatform 类型一起使用,但是在尝试传递可序列化数据时,我在运行时不断遇到同样的问题:

Caused by: java.lang.IllegalArgumentException: org.kotlin.mpp.mobile.models.MyModel is not Serializable or Parcelable.

在我的 nav_host.xml 中,我有以下内容:

<fragment
    android:id="@+id/aFragment"
    android:name="com.corp.myapp.main.aFragment"
    android:label="aFragment" >
    <action
        android:id="@+id/action_aFragment_to_bFragment"
        app:destination="@id/bFragment"
        app:popUpTo="@id/bFragment"
        app:popUpToInclusive="true">
        <argument
            android:name="myname"
            app:argType="org.kotlin.mpp.mobile.models.MyModel"
            app:nullable="true" />
    </action>
</fragment>

我现在使用的两种方法都得到了完全相同的异常。

第一个是使用 kotlinx-serialization 插件,其中我有以下类型:

package org.kotlin.mpp.mobile.models

import kotlinx.serialization.Serializable

@Serializable
data class MyModel(val first: String = "", val last: String = "")

由于第一个不起作用,我尝试的第二个是使用 java.io.Serializable 的扩展来制作特定于平台的 (JVM) 实现,其中包含以下内容:

commondataModels.kt:

package org.kotlin.mpp.mobile.models

expect class MyModel(first: String, last: String)

actualdataModels.kt:

package org.kotlin.mpp.mobile.models

import java.io.Serializable

actual data class MyModel actual constructor(val first: String, val last: String): Serializable

我正在使用生成的方向类在我的 Activity 中进行导航调用:

import org.kotlin.mpp.mobile.models.*


val user = MyModel("Bruce","Lee")
host.findNavController().navigate(AFragmentDirections.actionAFragmentToBFragment(user))

提前感谢您的任何建议!

注意:我可以通过传递带有 navigate API 的 Bundle 来使一切正常工作,但是我希望它与安全参数一起使用。

最佳答案

按照 CommonsWare 的建议,使用@Parcelize(并扩展为 Parcelable)

enter image description here

诀窍是确保 Parcelable 不会破坏您的通用代码,因为这是 Android 独有的概念。 为此,在您的公共(public)代码中添加

expect interface Parcelable

还有:

@UseExperimental(ExperimentalMultiplatform::class) @OptionalExpectation @Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.BINARY) expect annotation class Parcelize()

看起来像这样: enter image description here

然后在您的 Android 源集中,实际键入别名:

actual typealias Parcelable = android.os.Parcelable

actual typealias Parcelize = kotlinx.android.parcel.Parcelize

在其他源集中(比如iOS),直接把这个放到实际中:

actual interface Parcelable

关于android - 如何将 safe-args 与 MPP 可序列化数据一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61129335/

相关文章:

android-studio - 如何在 Android Studio 中为 Kotlin Multiplatform 配置 iOS 应用程序?

android - Ice Cream Sandwich 模拟器上的 Google 帐户

android - 实现内容提供者 : compound primary key

android - 应用程序没有关闭游标或数据库对象android

android - 为什么我的应用程序崩溃调用 "setupActionBarWithNavController(navController, appBarConfiguration)"

android - 导航架构组件 : transition animations not working for dialog

javascript - kotlin 多平台 : how to use Kotlin Flow in Javascript

android - 按四个按钮中的任何一个时我的应用程序崩溃

android - RecyclerView 与 ViewPager 中的 StaggeredGridLayoutManager,返回 fragment 时自动排列项目

ios - 卡住函数时 kotlin-native freeze() 方法崩溃