I have used autogeneration for Parcelable
implementation in a Kotlin data
class. The same class is used as an Entity
in Room
so I have default values for every constructor params to provide an empty constructor to Room
. When I try to pass this item as Parcelable
the app crashes with UnsupportedOperationException
and this message
Parcelables don't support default values
The work around for now I have , I just serialize the object with Gson
and pass it as String
but I would like to know if there is proper way to handle default values in Kotlin and Parcelable
.
Here is the class
@Entity
data class ProductData(
//primary key here
@ ColumnInfo val alcoholRestriction: Boolean = false,
@Ignore val averageWeight: Double = 0.0,
@Ignore val hotFoodRestriction: Boolean = false,
@Ignore val maxOrderValue: Double = 0.0,
@Ignore val minOrderValue: Double = 0.0,
@Ignore val orderIncrement: Double = 0.0,
@ColumnInfo var productId: String = "",
@Ignore val stdUomCd: String = "",
@Ignore val uomQty: String = ""
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readByte() != 0.toByte(),
parcel.readDouble(),
parcel.readByte() != 0.toByte(),
parcel.readDouble(),
parcel.readDouble(),
parcel.readDouble(),
parcel.readString(),
parcel.readString(),
parcel.readString()
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeByte(if (alcoholRestriction) 1 else 0)
parcel.writeDouble(averageWeight)
parcel.writeByte(if (hotFoodRestriction) 1 else 0)
parcel.writeDouble(maxOrderValue)
parcel.writeDouble(minOrderValue)
parcel.writeDouble(orderIncrement)
parcel.writeString(productId)
parcel.writeString(stdUomCd)
parcel.writeString(uomQty)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<ProductData> {
override fun createFromParcel(parcel: Parcel): ProductData {
return ProductData(parcel)
}
override fun newArray(size: Int): Array<ProductData?> {
return arrayOfNulls(size)
}
}
}
Trying to pass is in a bundle and the crash happens on this line.
findNavController().navigate(
Uri.parse("android-app://androidx.navigation/productDetails/$item"))
it is set in the nav graph as custom Parcelable variable