BundleCompat

public final class BundleCompat


Helper for accessing features in Bundle.

Summary

Public methods

static @Nullable IBinder

A convenience method to handle getting an IBinder inside a Bundle for all Android versions.

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:

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:

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:

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:

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:

static void
putBinder(
    @NonNull Bundle bundle,
    @Nullable String key,
    @Nullable IBinder binder
)

A convenience method to handle putting an IBinder inside a Bundle for all Android versions.

Public methods

getBinder

Added in 1.11.0
public static @Nullable IBinder getBinder(@NonNull Bundle bundle, @Nullable String key)

A convenience method to handle getting an IBinder inside a Bundle for all Android versions.

Parameters
@NonNull Bundle bundle

The bundle to get the IBinder.

@Nullable String key

The key to use while getting the IBinder.

Returns
@Nullable IBinder

The IBinder that was obtained.

getParcelable

Added in 1.10.0
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 null value 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 null

@NonNull Class<T> clazz

The type of the object expected

Returns
@Nullable T

a Parcelable value, or null

getParcelableArray

Added in 1.10.0
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 null value 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 null

@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 null

getParcelableArrayList

Added in 1.10.0
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 null value 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.
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 array list. This is only verified when unparceling.

Returns
@Nullable ArrayList<T>

an ArrayListvalue, or

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 null value is explicitly associated with the key.
  • The object is not of type clazz.
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 null

@NonNull Class<T> clazz

The type of the object expected

Returns
@Nullable T

a Serializable value, or null

getSparseParcelableArray

Added in 1.10.0
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 null value is explicitly associated with the key.
  • The object is not of type clazz.
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 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

putBinder

Added in 1.11.0
public static void putBinder(
    @NonNull Bundle bundle,
    @Nullable String key,
    @Nullable IBinder binder
)

A convenience method to handle putting an IBinder inside a Bundle for all Android versions.

Parameters
@NonNull Bundle bundle

The bundle to insert the IBinder.

@Nullable String key

The key to use while putting the IBinder.

@Nullable IBinder binder

The IBinder to put.