Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Bundle 'getSerializable(String?): Serializable?' is deprecated. Deprecated in Java

Tags:

android

enter image description here

/**
 * Returns the value associated with the given key, or {@code null} if:
 * <ul>
 *     <li>No mapping of the desired type exists for the given key.
 *     <li>A {@code null} value is explicitly associated with the key.
 *     <li>The object is not of type {@code clazz}.
 * </ul>
 *
 * @param key   a String, or null
 * @param clazz The expected class of the returned type
 * @return a Serializable value, or null
 */
@Nullable
public <T extends Serializable> T getSerializable(@Nullable String key,
        @NonNull Class<T> clazz) {
    return super.getSerializable(key, requireNonNull(clazz));
}

"My question is how to use Bundle's getSerializable method to retrieve collection objects."

like image 243
Sidnio Avatar asked Sep 27 '26 23:09

Sidnio


1 Answers

It's preferable to use Parcelable for Android rather than Serializable.

Hence, if you implement Parcelable for your TreeCatalogue class (easiest way is to use Parcelize)

Then you can use:

bundle.putParcelableArrayList("data", treeCatalogue)

When you create the bundle and:

val treeCatalogue = bundle.getParcelableArrayList<TreeCatalogue>("data")

When you restore it.

You can also use BundleCompat to get rid of the deprecation warning:

val treeCatalogue = BundleCompat.getParcelableArrayList(bundle, "data", TreeCatalogue::class.java)
like image 120
Philio Avatar answered Sep 30 '26 14:09

Philio