0

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?

Lee7355512727
  • 69
  • 2
  • 10

2 Answers2

1

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)
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
1

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.

G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40
  • Can you explain more in detail on how to "copy" the code that parcels an ArrayList? Bundle only has putParcelableArrayList method, but no putParcelableCopyOnWriteArrayList method. I'm still confused about how you would pass a CopyOnWriteArrayList with the putParcelableArrayList method. Thank you! – Lee7355512727 Aug 24 '14 at 05:14
  • You can't! Instead, in WriteToParcel, in a loop, pull things out of the array and put them in the parcel! – G. Blake Meike Aug 24 '14 at 15:31