Serializable or Parcelable? Why and Which one?

Before comparing Java Serializable and Android Pacelable together, let’s find the answer to this question. “In case of transferring objects between Android components, why do we have to convert them to the Serializable or Parcelable?”
When an application is in the background, its process may be killed by the OS in case of low memory. And by returning back the user to the app, the OS creates a new process for the application. Therefore, with passing an instance of the object to the Bundle, when the process is changed, the reference of the object won’t be in the new process and it wouldn’t be possible to use that object. Also, in the case of PendingIntents, if the owing application is killed, the PendingIntent itself will remain usable from other processes that have been given it.
So, the OS instead of saving the reference of an object must save its values and it would be possible just by converting that object to the Parcelable or Serializable.
Now that we know why we are forced to use either Parcelable or Serializable in this situation, it’s time to find out about them.
Serializable Objects
Serializing objects means converting an object’s state to a byte stream and deserializing means reverting back a byte stream into a copy of the object.
What are Serializable-Objects used for?
- Storing objects’ data into a file on disk.
- In games, the state of the game can be stored into a file on disk by using Serializable.
- Sending data over the network.
- Transferring objects between different components in Android.
How to create a Serializable object?
Creating a Serializable object is as simple as implementing the java.io.Serializable interface in your class (There are no methods to override).
That’s it. Now you can save your object into a file or send it to another Android component.
Parcelable
A parcel is highly optimized for local IPC (Inter-Process Communication — You can get familiar with IPC here) also is not attempt to save data in any kind of persistent storage.
How to create a Parcelable object?
Creating a parcelable object is not as simple as creating a Serializable object. First of all the class must implement the Parcelabel interface and fill all its required methods, then, our class must have a non-null static field called CREATOR with the type of Parcelable.Creator.
by making our User class parcelable it would be like below:
As you see if we want to make a class Parcelable, we have to write a lot of boilerplate codes but don’t worry, I have some good news for you.
If you’re still using Java in your project, you can rely on Android Studio to write all necessary codes for you. After implementing Parcelable in your class, move your mouse on the class name, press Alt+Entre, and select Add Parcelable Implementation option. By doing this, Android Studio writes all required codes for you.

And the thing goes easier if you are a Kotlin developer. First, we must add the kotlin-parcelize plugin to the app build.gradle file.
Then we can rely on Kotlin to create our Parcelable object by adding @Parcelize annotation to our class and implementing the Parcelable interface in it without overriding any methods:
Isn’t it cool? it’s just one reason why we must use Kotlin instead of Java :D
Conclusion
Now that we know how to create Parcelable and Serializable objects, in case of transferring objects between Android components which one is a better choice?
As we learned, in comparison with Serializable, making a Parcelable object requires a lot of boilerplate code, especially for java developers. But in Serializable, reflection is used and through the process, many temporary objects will be created. Thus, much memory will be used. On the other side, Parcelable is faster and more optimized than Serialization because it’s the developer’s responsibility to build the parcel object, so there is no need to use reflection.
But keep in mind that if you want to save an object in a file or save the state of your game into a file on the storage you still must use Serializable instead of Parcelable. Note this extract from the 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.
Summary
In this article, we’ve learned why and how to use Parcelable or Serializable objects in case of sending data between Android components. Below is a short summary of what we learned:
- Sending object references between Android components is not possible because by changing the process, the object references won’t be in the new process, so we must make our objects Parcelabel or Serializable till the OS be able to save the values of the objects
- We have two choices to transfer objects between Android components, that are making objects Parcelaber or Serializable
- 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