androidx.core:core-ktx:1.10.0 provides two classes IntentCompat
and BundleCompat
with few helpful static methods
androidx.core.os.BundleCompat
:
BundleCompat.getParcelable(Bundle, String, Class<T>)
BundleCompat.getParcelableArray(Bundle, String, Class<? extends Parcelable>): Parcelable[]
BundleCompat.getParcelableArrayList(Bundle, String, Class<? extends T> clazz): ArrayList<T>
BundleCompat.getSparseParcelableArray(Bundle, String, Class<? extends T>): SparseArray<T>
androidx.core.content.IntentCompat
IntentCompat.getParcelableExtra(Intent, String, Class<T>)
IntentCompat.getParcelableArrayExtra(Intent, String, Class<? extends Parcelable>): Parcelable[]
IntentCompat.getParcelableArrayListExtra(Intent, String, Class<? extends T>): ArrayList<T>
Example for retrieving an object of android.bluetooth.BluetoothDevice
:
IntentCompat.getParcelableExtra(intent, BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java)
Simplified version of extensions proposed by @Niklas:
inline fun <reified T : Parcelable> Bundle.parcelable(key: String): T? =
BundleCompat.getParcelable(this, key, T::class.java)
inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? =
IntentCompat.getParcelableExtra(this, key, T::class.java)
For ArrayList:
inline fun <reified T : Parcelable> Bundle.parcelableList(key: String): List<T>? =
BundleCompat.getParcelableArrayList(this, key, T::class.java)
inline fun <reified T : Parcelable> Intent.parcelableList(key: String): List<T>? =
IntentCompat.getParcelableArrayListExtra(this, key, T::class.java)
details
like samplecode
to help us fully understand your issue.