BundleKt

Added in 1.19.0

public final class BundleKt


Summary

Public methods

static final @NonNull Bundle

Returns a new empty Bundle.

static final @NonNull Bundle

This method is deprecated. This method does not provide type safety at compile time.

static final Parcelable[]
<T extends Parcelable> getParcelableArrayCompat(
    @NonNull Bundle receiver,
    @NonNull String key
)

Returns the value associated with the given key or null if:

static final ArrayList<@NonNull T>
<T extends Parcelable> getParcelableArrayListCompat(
    @NonNull Bundle receiver,
    @NonNull String key
)

Returns the value associated with the given key or null if:

static final T
<T extends Parcelable> getParcelableCompat(
    @NonNull Bundle receiver,
    @NonNull String key
)

Returns the value associated with the given key or null if:

static final T
<T extends Serializable> getSerializableCompat(
    @NonNull Bundle receiver,
    @NonNull String key
)

Returns the value associated with the given key or null if:

static final SparseArray<@NonNull T>
<T extends Parcelable> getSparseParcelableArrayCompat(
    @NonNull Bundle receiver,
    @NonNull String key
)

Returns the value associated with the given key or null if:

Public methods

bundleOf

public static final @NonNull Bundle bundleOf()

Returns a new empty Bundle.

bundleOf

public static final @NonNull Bundle bundleOf(@NonNull Pair<@NonNull String, Object>... pairs)

Returns a new Bundle with the given key/value pairs as elements.

Throws
IllegalArgumentException

When a value is not a supported type of Bundle.

getParcelableArrayCompat

public static final Parcelable[] <T extends Parcelable> getParcelableArrayCompat(
    @NonNull Bundle receiver,
    @NonNull String key
)

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 T.

Note: if the expected value is not a class provided by the Android platform, you must call Bundle.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 String key

a String, or null

Returns
Parcelable[]

an Array value, or null

getParcelableArrayListCompat

public static final ArrayList<@NonNull T> <T extends Parcelable> getParcelableArrayListCompat(
    @NonNull Bundle receiver,
    @NonNull String key
)

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 T.

Note: if the expected value is not a class provided by the Android platform, you must call Bundle.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 String key

a String, or null

Returns
ArrayList<@NonNull T>

an ArrayList value, or null

getParcelableCompat

public static final T <T extends Parcelable> getParcelableCompat(
    @NonNull Bundle receiver,
    @NonNull String key
)

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 T.

Note: if the expected value is not a class provided by the Android platform, you must call Bundle.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 String key

a String, or null

Returns
T

a Parcelable value, or null

getSerializableCompat

public static final T <T extends Serializable> getSerializableCompat(
    @NonNull Bundle receiver,
    @NonNull String key
)

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 T.

Compatibility behavior:

  • SDK 34 and above, this method matches platform behavior.

  • SDK 33 and below, the object type is checked after deserialization.

Parameters
@NonNull String key

a String, or null

Returns
T

a Serializable value, or null

getSparseParcelableArrayCompat

public static final SparseArray<@NonNull T> <T extends Parcelable> getSparseParcelableArrayCompat(
    @NonNull Bundle receiver,
    @NonNull String key
)

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 T.

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 String key

a String, or null

Returns
SparseArray<@NonNull T>

a SparseArray of T values, or null