serialization-parcelable icon indicating copy to clipboard operation
serialization-parcelable copied to clipboard

getParcelable returning null when it shouldnt

Open radimjanda754 opened this issue 1 year ago • 2 comments

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.

radimjanda754 avatar Jul 01 '24 18:07 radimjanda754

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

github-actions[bot] avatar Jul 01 '24 18:07 github-actions[bot]

I will try and take a deeper look to see what is going on when I have spare time. In the meantime, try the latest version 0.9.0.

chRyNaN avatar Aug 27 '24 20:08 chRyNaN