2

I know this question has been asked multiple number of times and i have been through a lot of these questions......almost all of these questions throw up the use of the parcelable interface for your class. However in a couple of questions i came across a quotation:

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

This quote can also be found in the book Pro Android 2.

Now seeing that all activities within the same application run in the same process(Every Activity in Android is a Process,or One Application is one process),unless otherwise specified in the manifest,communication within the activities of the same application is not Interprocess communication per se.So is it really faster to use the parcelable class or is it just enough to pass your object attributes through bundle via intent ?

Shedding any light on this aspect will be largely appreciated. Cheers !!

1
  • Personally i use class that extends from application and store them as static maybe these is not the best practice but it works great. Aug 30, 2011 at 6:31

3 Answers 3

8

There's a FAQ for that. :)

The short answer is that the Android team recommends three techniques for passing data between activities and services within an app: a singleton class; a public static field or method; a HashMap of WeakReferences to Objects (and you pass the key in the intent). The major issue to keep in mind is how your data is going to behave under various lifecycle events. (For instance, if the user turns the phone, by default your activities will be destroyed and recreated; your data handling method needs to be designed with that in mind.)

4
  • @ted...i am using static field to pass data between all activities and services..its working all ok...i dont know is it a better approach.Can u guide me on this Dec 27, 2018 at 11:39
  • @SyedDanishHaider - That's one of the techniques that Google recommends. If it's working ok, I'd say don't bother trying to find a better approach. There's an American proverb: If it ain't broke, don't fix it. It sounds like it applies here.
    – Ted Hopp
    Dec 27, 2018 at 14:40
  • yep its working ok but i have heard from many its not better approach. Dec 30, 2018 at 7:46
  • 1
    @SyedDanishHaider - I've found only two issues with using a static field. First, the field value is not subject to garbage collection as long as the class with the field is loaded in memory. If you no longer need the value, you have to manually set the field to null. Second, you have to plan for the possibility that the system will kill off your process while the app is in the background and you will have to reinitialize the field when the app starts up again. As long as your code addresses both of these issues, there's no need for a better approach.
    – Ted Hopp
    Dec 30, 2018 at 13:24
5

The Parcelable construct is designed to be very fast at passing data across application memory boundaries: within an application you are MUCH better served using Bundle because all the memory locations the data is stored in are accessible to both the sender and the receiver. Since the in-memory objects are accessible there is no need to incur the cost of reconstruction: just use the Bundle, which is really just a glorified HashMap with type-specific put/get methods.

For AIDL and IPC purposes you can't (by default) share memory locations so you need an efficient way of moving data: this is where Parcelable kicks in. Unless one of the components of your application is using the remote process capability then there is no need to use Parcelable.

1
  • I thought so too ! But Ted hop up there provided an excellent link to solve it ! Cheers anyway !
    – Ashwin
    Aug 30, 2011 at 6:47
1

From docs:

Parcelables

The Parcelable protocol provides an extremely efficient (but low-level) protocol for objects to write and read themselves from Parcels. You can use the direct methods writeParcelable(Parcelable, int) and readParcelable(ClassLoader) or writeParcelableArray(T[], int) and readParcelableArray(ClassLoader) to write or read. These methods write both the class type and its data to the Parcel, allowing that class to be reconstructed from the appropriate class loader when later reading.

There are also some methods that provide a more efficient way to work with Parcelables: writeTypedArray(T[], int), writeTypedList(List), readTypedArray(T[], Parcelable.Creator) and readTypedList(List, Parcelable.Creator). These methods do not write the class information of the original object: instead, the caller of the read function must know what type to expect and pass in the appropriate Parcelable.Creator instead to properly construct the new object and read its data. (To more efficient write and read a single Parceable object, you can directly call Parcelable.writeToParcel and Parcelable.Creator.createFromParcel yourself.)

Bundles

A special type-safe container, called Bundle, is available for key/value maps of heterogeneous values. This has many optimizations for improved performance when reading and writing data, and its type-safe API avoids difficult to debug type errors when finally marshalling the data contents into a Parcel. The methods to use are writeBundle(Bundle), readBundle(), and readBundle(ClassLoader).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.