Issue
Starting from API level 33 the getSerializable(String)
method of Bundle class is deprecated. Documentation suggests to use getSerializable(String, Class)
instead. But that function is only available from API level 33.
val model = args.getSerializable("key") as? Model
Is this how it should be now?
val model = args.customGetSerializable<Model>("key")
@Suppress("DEPRECATION")
inline fun <reified T : Serializable> Bundle.customGetSerializable(key: String): T? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getSerializable(key, T::class.java)
} else {
getSerializable(key) as? T
}
}
Solution
Is this how it should be now?
Yes.
Ideally, Google would add stuff to BundleCompat
for these changes, and perhaps they will now that Android 13 is starting to ship to users.
Answered By - CommonsWare
Answer Checked By - Cary Denson (JavaFixing Admin)