Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is parcelable worth to implement to pass data between activities?

I want to improve the performance of my app. One thing which comes to my mind is to replace all the serializable intent-bundle parameters with parcelable.

I found this thread: Benefit of using Parcelable instead of serializing object

There somebody states, from a book, that parcelable is only meant to be used for interprocess communication? That's probably not up to date, anymore, right? Because it is, technically at least, possible.

Then there's also the statement that parcelable is not reliable because the implementation varies accross devices, this would be of course a killing characteristic, since I want it to work always, all devices and apis, starting from API 7.

And I also read some advices about using Externalizable or implementing a custom protocol... but I don't understand, why Parcelable is not reliable, why Android developers do such buzz around something which will not work on all devices? Or is the comment not true?

Is it worth to implement Parcelable? Do I stick with Serializable? Or is a custom serialization / Externalizable the right approach?

Please don't tell me "just try", I don't have time for this, specially to check if Parcelable is reliable (and also not enough devices)... experience is asked...

Thanks.

P.S. Also don't say me "Serializable will be acceptable for most cases" I know that, it's indeed acceptable but I have time and would like to use it with Parcelable (or something else), if this improves the user experience.

like image 760
User Avatar asked Aug 16 '12 19:08

User


People also ask

Why do we use Parcelable?

Parcelable and Bundle objects are intended to be used across process boundaries such as with IPC/Binder transactions, between activities with intents, and to store transient state across configuration changes.

How do you pass data between activities?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

Which is better Serializable or Parcelable?

Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn't create more temp objects while passing the data between two activities.

Why Parcelable is faster than Serializable?

In Parcelable, developers write custom code for marshaling and unmarshaling so it creates less garbage objects in comparison to Serialization. The performance of Parcelable over Serialization dramatically improves (around two times faster), because of this custom implementation.


2 Answers

Parcelable works always, all the time, on all devices. If it didn't, nothing would work. Android internals rely very heavily on Parcelable.

Parcelable will be a bit more efficient than Serializable but I seriously doubt that it would have much impact on your "user experience" (unless, of course, you are using it all over the place and serializing very large and complicated objects).

If you think you are having performance issues, then I would spend my available time profiling the application and gathering empirical data about where it is spending its time. IMHO replacing Serializable with Parcelable is a relatively low-level implementation optimization that is likely to give you close to zero perceivable performance improvement.

like image 162
David Wasser Avatar answered Nov 14 '22 23:11

David Wasser


There somebody states, from a book, that parcelable is only meant to be used for interprocess communication? That's probably not up to date, anymore, right? Because it is, technically at least, possible.

If Parcelable was not meant to be passed between activities, why does the putExtra (String name, Parcelable value) Intent method exists since API Level 1?

Then there's also the statement that parcelable is not reliable because the implementation varies accross devices, this would be of course a killing characteristic, since I want it to work always, all devices and apis, starting from API 7.

Let my quote the Parcel documentation:

Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

Here I can only read an advice about not storing a Parcel data into persistent storage. From my personal experience Parcelable was never an issue for passing data between activities. I heavily use it and support API 8 to current API.

Concerning performance and speed it's established that using Parcelable is better, as it is an Android specific high-performance IPC transport serialization mechanism. Though, in order to actually see an improvement, it should depends on how much you use it.

like image 41
Michel-F. Portzert Avatar answered Nov 14 '22 22:11

Michel-F. Portzert