Phate
Phate

Reputation: 6622

Pass a custom object list to an activity: parcelable or serializable?

From android documentation:

NOTE: Seeing Parcelable might have triggered the question, why is Android not using the built-in Java serialization mechanism? It turns out that the Android team came to the conclusion that the serialization in Java is far too slow to satisfy Android’s interprocess-communication requirements. So the team built the Parcelable solution. The Parcelable approach requires that you explicitly serialize the members of your class, but in the end, you get a much faster serialization of your objects.

So we know that Parcelable is actually better than Serializable but, on the other hand,

Also realize that Android provides two mechanisms that allow you to pass data to another process. The first is to pass a bundle to an activity using an intent, and the second is to pass a Parcelable to a service. These two mechanisms are not interchangeable and should not be confused. That is, the Parcelable is not meant to be passed to an activity. If you want to start an activity and pass it some data, use a bundle. Parcelable is meant to be used only as part of an AIDL definition.

Ok man, but I need to pass a custom object list to my activity!So into the bundle I still have to put either a parcelable or a serializable!

For now I did this way:

public class MyObject implements Serializable{

and to pass:

 Bundle b = new Bundle();
 b.putSerializable("objList", anArrayListWithMyObjectElements);
 intent.putExtra("objList", b);

as ArrayList implements Serializable too it works fine...but I don't see the point of using a bundle this way...but android tells me to not use Parcelable for activity communication...what's the correct answer??

Upvotes: 1

Views: 1660

Answers (1)

m0skit0
m0skit0

Reputation: 25873

In my exprience, I strongly suggest using Parcelable when you can, IPC or not. It's Android's de facto replacement for Java's Serializable, and it's more optimized.

If you need help on how to parcel an object, let me know.

Upvotes: 3

Related Questions