Search code examples
androidkotlinandroid-intentdeprecation-warningandroid-tiramisu

getSerializableExtra and getParcelableExtra are deprecated. What is the alternative?


I have upgraded targetSdkVersion and compileSdkVersion to 33.

I am now getting warnings telling me that the getSerializableExtra(name:) and getParcelableExtra(name:) methods are deprecated.

I checked and confirmed from the documentation in the Intent class that these two methods are indeed deprecated.

The documentation suggests that I use the new getSerializableExtra(name:clazz:) and getParcelableExtra(name:clazz:) methods instead.

Can anyone can help me use the new methods?

Example warnings

  1. Warning while getting an Object:

getSerializableExtra_Deprecated_Image

  1. Warning while getting a List or ArrayList:

getSerializableExtra_Deprecated_List_Image


Solution

  • This is what I use:

    inline fun <reified T : Serializable> Bundle.serializable(key: String): T? = when {
      Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializable(key, T::class.java)
      else -> @Suppress("DEPRECATION") getSerializable(key) as? T
    }
    
    inline fun <reified T : Serializable> Intent.serializable(key: String): T? = when {
      Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializableExtra(key, T::class.java)
      else -> @Suppress("DEPRECATION") getSerializableExtra(key) as? T
    }
    

    I've also written the same for getParcelable here and requested this to be added to the Support libraries directly