After some time using the implementation Serializable in my classes on Java (Android) I discovered the Parcelable, but I couldn't find out when to choose one or another. Also what are the performance differences between them?
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.
Unlike Serializable, in Parcelable reflection won't be used so it is faster and has better performance. Android Parcelabe is not made to save objects into the files, so if you want to save an object in the file you must use Serializable.
Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations. Parcelable is an Android specific interface where you implement the serialization yourself.
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.
what are the performance differences between them?
Parcelable
is signficantly faster than is Serializable
for the places where you use Parcelable
: Intent
extras, saved instance state Bundle
, etc.
That being said, assuming that you are not doing lots of this stuff, particularly in tight loops, users are unlikely to really notice the difference.
when to choose one or another
If you are doing Android development, prefer Parcelable
to Serializable
where Parcelable
is an option. At most, use Serializable
for data persistence, though I would recommend other serialization options even for that (e.g., JSON via Gson).
The one primary exception to this would be if your Java objects are in a separate library that would be used both from Android and from other Java environments. Those environments will not have Parcelable
, but Serializable
would work in both places.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With