getParcelableArrayListExtra deprecated what is the alternative?

2.6k views Asked by At

I have upgraded targetSdkVersion and compileSdkVersion to 33.

Now I am getting this warning enter image description here

val picList =
           result.data?.getParcelableArrayListExtra<PageNumberFile>(KEY_CAM_PIC_LIST)

It suggest me use Use the type-safer, What is the solution?

2

There are 2 answers

2
snachmsm On

Your best guide would be DOC

This method was deprecated in API Level 33. Use the type-safer getParcelableArrayListExtra(java.lang.String, java.lang.Class) starting from Android Build.VERSION_CODES#TIRAMISU.

so you should use THIS getParcelableArrayListExtra(String name, Class<? extends T> clazz). Note second argument, in your case it should be PageNumberFile::class.java

PS. I would post working snippet/line, but you have posted code as image and I can't copy it for improving and pasting in my answer and I won't be rewritting this, too lazy. Don't ever post text as not-copyable image!

0
Marek Macko On

androidx.core:core-ktx provides IntentCompat and androidx.core.os.BundleCompat class, with the following method:

IntentCompat.getParcelableArrayListExtra(result.data, KEY_CAM_PIC_LIST, PageNumberFile::class.java)

To make it more convenient, you can create extensions:

inline fun <reified T : Parcelable> Bundle.parcelableList(key: String): List<T>? =
    BundleCompat.getParcelableArrayList(this, key, T::class.java)

inline fun <reified T : Parcelable> Intent.parcelableList(key: String): List<T>? =
    IntentCompat.getParcelableArrayListExtra(this, key, T::class.java)