How to pass a CopyOnWriteArrayList to a Bundle object?

242 Views Asked by At

I have a data structure MyObject that implements the Parcelable interface, and I want to pass a CopyOnWriteArrayList<MyObject> object to a Bundle object to create a new Fragment. So I tried

Bundle args = new Bundle();
args.putParcelableArrayList("RssItems", rssItems);

But since CopyOnWriteArrayList is not a subclass of ArrayList, it does not match the method signature.

Is there any way to pass a CopyOnWriteArrayList to a Bundle object?

2

There are 2 best solutions below

2
On BEST ANSWER

Think about it this way:

CopyOnWriteArrayList is simply a very special implementation of a list. You don't actually care about Parceling the implementation, you just care about Parceling up the contents of the list.

If you look at the Parcelable interface, you'll see that that is dead simple. In fact, you can probably copy the code (from AOSP) that parcels an ArrayList, directly.

While it is true that you can implement Serializable, it is no easier than implementing Parcelable and it is way slower.

0
On

you can use the Serializable instead to write the object to the bundle.

sample:

You need to just implement Serializable in your object

public class CopyOnWriteArrayList implements Serializable

Putting and getting it in the Bundle:

args.putSerializable(key, value)
args.getSerializable(key)