Issue
Since getParcelable is deprecated for Android Tiramisu and later i want to make an extension function to get the parcelables from my Intent through my Bundle
So i have
val server = if (Build.VERSION.SDK_INT >= 33) {
it.getParcelable("server", Server::class.java)
} else {
it.getParcelable("server")
}
And i want to create an extension for every model like this
fun <T> T.getParcelable(bundle: Bundle, key: String): T? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
bundle.getParcelable(key, this!!::class.java)
else
bundle.getParcelable(key)
However, when i try to use it i get an unresolved reference error
How can i fix that?
Solution
Something like this?
inline fun <reified T : Any> KClass<T>.getParcelable(bundle: Bundle, key: String): T? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
bundle.getParcelable(key, T::class.java)
else
bundle.getParcelable(key)
And you use it like this
val server = Server::class.getParcelable(bundle, "server")
Answered By - Leonardo Velozo
Answer Checked By - Candace Johnson (JavaFixing Volunteer)