Parcelables 不支持默认值.Android 导航深层链接参数 [英] Parcelables don't support default values. Android navigation deeplink argument
问题描述
在执行传参解决方案的过程中,在模块之间导航时,遇到了一个序列化错误.据我所知,深度链接接受自定义参数类型,即 Parcelables 或 Serializable.
During the implementation of the passing parameter solution, in navigation between modules, I came across a serialization error. Deeplinks, as far as I know, accepts custom argument types, which are Parcelables or Serializable.
我正在使用最新版本的导航 2.2.0
Im using newest version of navigation 2.2.0
错误信息:
java.lang.UnsupportedOperationException: Parcelables 不支持默认值.
java.lang.UnsupportedOperationException: Parcelables don't support default values.
是我做错了什么还是仍在开发中?
Am I doing something wrong or this is still under development?
这是一个简短的例子:
<fragment
android:id="@+id/sampleFragment"
android:name="com.testapp.app.samples.navigation.SampleFragment"
android:label="SampleFragment">
<argument
android:name="Args"
app:argType="com.testapp.navigation.SampleArgs" />
<deepLink app:uri="app://app/samples/navigation/SampleFragment?Args={Args}"/>
</fragment>
@Parcelize
@Keep data class SampleArgs(
val text: String
) : NavArgs, Parcelable
val x = SampleArgs("TEST")
val uri = Uri.parse("app://app/samples/navigation/SampleFragment?Args=$x")
navController.navigate(uri)
我在这里找到了类似的东西 Android Parcelable don't 支持默认值 App Crash
I found something similar here Android Parcelable don't support default values App Crash
这是我在堆栈上的第一篇文章,所以请温柔:)
It's my first post on stack, so please be gentle :)
编辑
这里是答案:
https://issuetracker.google.com/issues/148523779
推荐答案
Parcelables 目前不支持默认值,因此您需要将对象作为 String 值传递.是的,这是一种解决方法.因此,我们可以将该对象转换为 JSON(字符串)并通过导航传递,然后将该 JSON 解析回目的地的对象,而不是将对象本身作为 Parcelize 对象传递.您可以使用 GSON
进行对象到 json-string 的转换.
Parcelables currently don't support default values so you need to pass your object as String value. Yes it is a work around.. So instead of passing object itself as Parcelize object we can turn that object into JSON (String) and pass it through navigation and then parse that JSON back to Object at destination. You can use GSON
for object to json-string conversion.
这篇关于Parcelables 不支持默认值.Android 导航深层链接参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!