chRyNaN / serialization-parcelable

Android Parcelable support for the Kotlinx Serialization library.
Apache License 2.0
69 stars 3 forks source link

getParcelable returning null when it shouldnt #9

Open radimjanda754 opened 3 weeks ago

radimjanda754 commented 3 weeks ago

Hello after upgrading from 0.5.1 to 0.7.1 I get many errors that serializable is null, when trying to access it after some decoding , but it shouldnt be null, Im not sure what am I doing wrong.

Here is example of the code:

@ExperimentalSerializationApi
inline fun <reified T> Bundle.putParcelableTest(
    key: String, value: T, parcelable: com.chrynan.parcelable.core.Parcelable = com.chrynan.parcelable.core.Parcelable.Default
) {
    val encoded = parcelable.encodeToBundle(value = value)
    putBundle(key, encoded)

    // This works
    val bundle = getBundle(key)
    val decoded: T = parcelable.decodeFromBundle(requireNotNull(bundle))

    // Returns null
    val decoded2: T? = getParcelable(key, parcelable = com.chrynan.parcelable.core.Parcelable.Default)

    // This works
    val decoded3: T? = getParcelableTest(key, parcelable = com.chrynan.parcelable.core.Parcelable.Default)
}

@ExperimentalSerializationApi
inline fun <reified T : Any> Bundle.getParcelableTest(
    key: String,
    flags: Int = 0,
    parcelable: com.chrynan.parcelable.core.Parcelable = com.chrynan.parcelable.core.Parcelable.Default
): T? {
    val bundle = getBundle(key)
    return parcelable.decodeFromBundle(requireNotNull(bundle))
}

The getParcelable bundle extension doesnt work here and returns null, however I realized that if I create my own extension that is reified T : Any instead of just reified T then it starts to work again. But this is not the only function which I have problem with, so Im not sure what am I doing wrong.

github-actions[bot] commented 3 weeks ago

Welcome and thanks for contributing! Make sure to leave a detailed explanation of the issue. For a faster response time, consider boosting your issue.