Parcelable vs Serializable
Parcelable vs Serializable 관련
Info
Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.
In this blog, we will learn about the difference between Parcelable
and Serializable
. We will also learn how they work internally.
Let's start with a scenario: Suppose in Android, we need to pass the following Developer
object between activities or fragments using intents or bundles.
data class Developer(val name: String, val age: Int)
We can't simply pass this Developer
object between activities or fragments using intents or bundles.
We have two solutions as follows:
Implement Parcelable
data class Developer(val name: String, val age: Int) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readInt()
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(name)
parcel.writeInt(age)
}
// code removed for brevity
companion object CREATOR : Parcelable.Creator<Developer> {
override fun createFromParcel(parcel: Parcel): Developer {
return Developer(parcel)
}
override fun newArray(size: Int): Array<Developer?> {
return arrayOfNulls(size)
}
}
}
A lot of boilerplate code, right?
To avoid this, we can use the kotlin-parcelize
plugin and annotate the class with @Parcelize
.
@Parcelize
data class Developer(val name: String, val age: Int) : Parcelable
When we annotate a class with @Parcelize
, the implementation is automatically generated, similar to what we wrote above.
No boilerplate code is needed now. They are automatically generated now.
This was the first solution: By implementing Parcelable
.
Now, let's discuss the second solution: By implementing Serializable
.
Implement Serializable
data class Developer(val name: String, val age: Int) : Serializable
Both of the above-mentioned solutions work, but they differ in terms of performance. We will discuss this below.
So, in Android development, both Parcelable
and Serializable
are the interfaces used to serialize objects so they can be passed between activities or fragments using intents or bundles.
Let's understand the difference between Parcelable
and Serializable
.
Parcelable
Parcelable is an Android-specific interface designed to serialize objects to pass them through Intent and Bundle .
When we implement Parcelable, we either write the implementation manually or use a plugin that generates it for us.
It does NOT use reflection, and it creates fewer temporary objects during the serialization process, which reduces garbage collection overhead.
Two things to note:
- Parcelable does NOT use reflection.
- Parcelable is an Android-specific interface.
So, Parcelable is faster than Serializable.
Serializable
A serializable interface in Java is a marker interface that allows an object to be converted into a stream of bytes (serialized) and then reconstructed back into an object (deserialized).
It is easy to implement since it requires the class to simply implement the Serializable
interface, which is a marker interface with no methods to override.
It uses reflection, and it creates many temporary objects, leading to higher memory usage and potential performance issues.
So, Serializable
is slower than Parcelable.
We now understand the differences between Parcelable and Serializable
.
Let me tabulate the difference between Parcelable and Serializable
.
Parcelable | Serializable |
---|---|
Android-Specific. | Java. |
Faster. | Slower. |
Less Memory Usage. | More Memory Usage. |
This was all about Parcelable
and Serializable
.
Prepare yourself for Android Interview: Android Interview Questions (amitshekhariitbhu/android-interview-questions
)
That's it for now.
Thanks
Amit Shekhar
You can connect with me on:
Follow Outcome School on: