I am passing Parcelable data into an Intent and getting it out on the other end using the getParcelableExtra(name:) method. However, getParcelableExtra(name:) seems to be deprecated. How do I fix the deprecation warning? Alternatively, are there any other options for doing this? I am using a compileSdkVersion
value of 33.
var data = intent.getParcelableExtra("data")
Here are two extension methods that I use for Bundle
& Intent
:
inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? = when {
SDK_INT >= 33 -> getParcelableExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableExtra(key) as? T
}
inline fun <reified T : Parcelable> Bundle.parcelable(key: String): T? = when {
SDK_INT >= 33 -> getParcelable(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelable(key) as? T
}
I also requested this to be added to the support library
And if you need the ArrayList support there is:
inline fun <reified T : Parcelable> Bundle.parcelableArrayList(key: String): ArrayList<T>? = when {
SDK_INT >= 33 -> getParcelableArrayList(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableArrayList(key)
}
inline fun <reified T : Parcelable> Intent.parcelableArrayList(key: String): ArrayList<T>? = when {
SDK_INT >= 33 -> getParcelableArrayListExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableArrayListExtra(key)
}
Note: There are some issues on SDK 33 with the new methods, so you might only want to use it starting from SDK 34.