Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 997
Nikron Avatar asked Jan 29 '20 21:01

Nikron


People also ask

How do you pass arguments in navigation component?

In the Navigation editor, click on the destination that receives the argument. In the Attributes panel, click Add (+). In the Add Argument Link window that appears, enter the argument name, argument type, whether the argument is nullable, and a default value, if needed. Click Add.

What is Safe Args?

Safe Args is a Gradle plug-in that generates code to add data to a Bundle and get it back in a simple and type-safe manner. Now that you see why Safe Args is useful for your project, it's time to implement it.


1 Answers

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.

like image 120
abhi Avatar answered Sep 21 '22 23:09

abhi