Parcelables don't support default values. Android navigation deeplink argument
Asked Answered
S

2

25

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.

Im using newest version of navigation 2.2.0

Error message:

java.lang.UnsupportedOperationException: Parcelables don't support default values.

Am I doing something wrong or this is still under development?

Here is short example:

<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)

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 :)

EDIT

Here is answer:

https://issuetracker.google.com/issues/148523779

Samella answered 29/1, 2020 at 21:4 Comment(0)
P
9

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.

Philanthropic answered 16/6, 2021 at 19:30 Comment(0)
A
-2

I know this is common pattern for any standard application. But I would like to stick to the Android recommendation which will make the design more simple and less complex. If you are passing complex data across a screen there is something you need to improve little bit on your design.

As per the following documentation https://developer.android.com/jetpack/compose/navigation#retrieving-complex-data

It is strongly advised not to pass around complex data objects when navigating, but instead pass the minimum necessary information, such as a unique identifier or other form of ID, as arguments when performing navigation actions:

// Pass only the user ID when navigating to a new destination as argument
navController.navigate("profile/user1234")

Complex objects should be stored as data in a single source of truth, such as the data layer. Once you land on your destination after navigating, you can then load the required information from the single source of truth by using the passed ID

I think it really makes sense. So what I will do in your place is to change the design accordingly and you will see the benefits later... :)

Actinium answered 6/1 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.