Serializable vs Parcelable

Passing around Strings and Integers in between activities can be done easily using Intents via the putExtras and getExtras methods, but if we need to pass object references we need to serialize them first.

Java offers a simple solution : Just implement the Serializable interface and Java will make its best effort to Serialize this class. This is simple and avoids a bunch of boilerplate code but is very bad for performance especially when we have 100’s of objects (A realistic case).  The reasons why are as follows :

  1. Serialization uses reflection to learn the variables and method names. Reflection tends to be slow.
  2. Serialization also creates a lot of temporary variables thereby necessitates garbage collection,  Which again will lead to noticeable jankiness in the UI based on when the GC is triggered.

Android decided this was too slow for the purposes of Android and implemented Parcelable which explicitly spells out how and which values need to serialized.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s