Status Update
Comments
cl...@google.com <cl...@google.com> #3
Details
is a route argument and unlike primitives arguments with built-in support (i.e String, Int), you will need to provide a custom NavType through the typeMap
parameter when building your NavGraph.
For example, something like
val detailsNavType = object: NavType<Details>(isNullableAllowed = false) {
override fun put(bundle: Bundle, key: String, value: Details) {
bundle.putString(key, "${value.name}.${value.description}")
}
override fun get(bundle: Bundle, key: String): Details {
val params = bundle.getString(key)!!.split('.')
return Details(name = params.first(), description = params.last())
}
override fun parseValue(value: String): Details {
val params = value.split('.')
return Details(name = params.first(), description = params.last())
}
override fun serializeAsValue(value: Details): String {
return Uri.encode("${value.name}.${value.description}")
}
}
The fields can also be parsed in other ways, i.e. with Json.
For better support on custom argument types, follow
Description
Component used: Navigation 2.8.3
When using a serializable route, it cannot have nested serializable objects, as the navigation library throws an error. See this example below:
Declaring this route will throw this error:
java.lang.IllegalArgumentException: Route com.example.Route could not find any NavType for argument details of type com.example.Details - typeMap received was {}
I realize we can pass a
typeMap
when calling the method, but there is noNavType
to match the Kotlin classes annotated with@Serializable
.Also I think we should not need to pass a
typeMap
. Since the object is serializable, then we don't need to do anything further than that.For now, the workarounds I see are either:
Parcelable
since there's aNavType
for that. Still not ideal either, and I'm actually seeing another issue related to that, that I'm debugging at the moment. So it doesn't look reliable.I hope you can improve on this, as it would make the library much simpler to use. Thank you.