BundleCompat
public final class BundleCompat
Helper for accessing features in Bundle.
Summary
Public methods |
|
|---|---|
static @Nullable IBinder |
@ReplaceWith(expression = "bundle.getBinder(key)")This method is deprecated. Use |
static @Nullable T |
Returns the value associated with the given key or |
static @Nullable Parcelable[] |
getParcelableArray(Returns the value associated with the given key, or |
static @Nullable ArrayList<T> |
Returns the value associated with the given key, or |
static @Nullable T |
<T extends Serializable> getSerializable(Returns the value associated with the given key or |
static @Nullable SparseArray<T> |
Returns the value associated with the given key, or |
static void |
@ReplaceWith(expression = "bundle.putBinder(key, binder)")This method is deprecated. Use |
Public methods
@ReplaceWith(expression = "bundle.getBinder(key)")
public static @Nullable IBindergetBinder(@NonNull Bundle bundle, @Nullable String key)
A convenience method to handle getting an IBinder inside a Bundle for all Android versions.
getParcelable
public static @Nullable T <T> getParcelable(
@NonNull Bundle in,
@Nullable String key,
@NonNull Class<T> clazz
)
Returns the value associated with the given key or null if:
- No mapping of the desired type exists for the given key.
- A
nullvalue is explicitly associated with the key. - The object is not of type
clazz.
Note: if the expected value is not a class provided by the Android platform, you must call setClassLoader with the proper ClassLoader first. Otherwise, this method might throw an exception or return null.
Compatibility behavior:
- SDK 34 and above, this method matches platform behavior.
- SDK 33 and below, the object type is checked after deserialization.
| Parameters | |
|---|---|
@NonNull Bundle in |
The bundle to retrieve from. |
@Nullable String key |
a String, or |
@NonNull Class<T> clazz |
The type of the object expected |
| Returns | |
|---|---|
@Nullable T |
a Parcelable value, or |
getParcelableArray
public static @Nullable Parcelable[] getParcelableArray(
@NonNull Bundle in,
@Nullable String key,
@NonNull Class<Parcelable> clazz
)
Returns the value associated with the given key, or null if:
- No mapping of the desired type exists for the given key.
- A
nullvalue is explicitly associated with the key. - The object is not of type
clazz.
Note: if the expected value is not a class provided by the Android platform, you must call setClassLoader with the proper ClassLoader first. Otherwise, this method might throw an exception or return null.
Compatibility behavior:
- SDK 34 and above, this method matches platform behavior.
- SDK 33 and below, this method will not check the array elements' types.
| Parameters | |
|---|---|
@NonNull Bundle in |
The bundle to retrieve from. |
@Nullable String key |
a String, or |
@NonNull Class<Parcelable> clazz |
The type of the items inside the array. This is only verified when unparceling. |
| Returns | |
|---|---|
@Nullable Parcelable[] |
a Parcelable[] value, or |
getParcelableArrayList
public static @Nullable ArrayList<T> <T> getParcelableArrayList(
@NonNull Bundle in,
@Nullable String key,
@NonNull Class<T> clazz
)
Returns the value associated with the given key, or null if:
- No mapping of the desired type exists for the given key.
- A
nullvalue is explicitly associated with the key. - The object is not of type
clazz.
Note: if the expected value is not a class provided by the Android platform, you must call setClassLoader with the proper ClassLoader first. Otherwise, this method might throw an exception or return null.
Compatibility behavior:
- SDK 34 and above, this method matches platform behavior.
- SDK 33 and below, this method will not check the list elements' types.
getSerializable
public static @Nullable T <T extends Serializable> getSerializable(
@NonNull Bundle in,
@Nullable String key,
@NonNull Class<T> clazz
)
Returns the value associated with the given key or null if:
- No mapping of the desired type exists for the given key.
- A
nullvalue is explicitly associated with the key. - The object is not of type
clazz.
- SDK 34 and above, this method matches platform behavior.
- SDK 33 and below, the object type is checked after deserialization.
| Parameters | |
|---|---|
@NonNull Bundle in |
The bundle to retrieve from. |
@Nullable String key |
a String, or |
@NonNull Class<T> clazz |
The type of the object expected |
| Returns | |
|---|---|
@Nullable T |
a Serializable value, or |
getSparseParcelableArray
public static @Nullable SparseArray<T> <T> getSparseParcelableArray(
@NonNull Bundle in,
@Nullable String key,
@NonNull Class<T> clazz
)
Returns the value associated with the given key, or null if:
- No mapping of the desired type exists for the given key.
- A
nullvalue is explicitly associated with the key. - The object is not of type
clazz.
- SDK 34 and above, this method matches platform behavior.
- SDK 33 and below, this method will not check the array elements' types.
| Parameters | |
|---|---|
@NonNull Bundle in |
The bundle to retrieve from. |
@Nullable String key |
a String, or null |
@NonNull Class<T> clazz |
The type of the items inside the sparse array. This is only verified when unparceling. |
| Returns | |
|---|---|
@Nullable SparseArray<T> |
a SparseArray of T values, or null |