Home > Back-end >  Android 13 (SDK 33): Bundle.getSerializable(String) is deprecated, what is alternative?
Android 13 (SDK 33): Bundle.getSerializable(String) is deprecated, what is alternative?

Time:08-17

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
    }
}

CodePudding user response:

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.

  • Related