Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 13 (SDK 33): Bundle.getSerializable(String) is deprecated, what is alternative?

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.

My current code:

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
    }
}
like image 319
Marat Avatar asked Sep 12 '25 14:09

Marat


1 Answers

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.

like image 186
CommonsWare Avatar answered Sep 15 '25 02:09

CommonsWare