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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With