Topic: 16 Understanding Parcebale and Serializable
This is our sixteenth topic from learn android from basic to advance series
Hello devs, Today's topic is Parcebale and Serializable interfaces. Both Serializable and Parcelable interfaces are used to transfer data between components in Android.
Serializable Interface:
Serializable is a standard Java interface. Serialization is the process of converting an object into a stream of bytes to store an object in memory so that it can be recreated at a later time, while still keeping the objectβs original state and data.
In this approach, you simply mark a class Serializable by implementing the interface and Java will automatically serialize it.
Here's how you would use Serializable in Android:
Implement the Serializable Interface:
import java.io.Serializable data class UserData(val name: String, val age: Int) : Serializable
Sending Data:
In the sending activity, you create an instance of your Serializable object and add it as an extra to an Intent:
val userData = UserData("John", 25) val intent = Intent(this, ReceiverActivity::class.java) intent.putExtra("userData", userData) startActivity(intent)
Receiving Data:
In the receiving activity, you retrieve the Serializable object from the Intent extras:
val userData = intent.getSerializableExtra("userData") as? UserData
Parcelable Interface:
Parcelable is an Android-specific interface that allows objects to be serialized and deserialized more efficiently than Serializable. Also, it is faster because it is optimized for usage in the development of Android, and shows better results. You must implement Parcelable interface methods to serialize and deserialize your object.
Here's how you would use Parcelable in Android:
Implement the Parcelable Interface:
import android.os.Parcel import android.os.Parcelable data class UserData(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) } override fun describeContents(): Int { return 0 } companion object CREATOR : Parcelable.Creator<UserData> { override fun createFromParcel(parcel: Parcel): UserData { return UserData(parcel) } override fun newArray(size: Int): Array<UserData?> { return arrayOfNulls(size) } } }
Sending Data:
In the sending activity, you create an instance of your Parcelable object and add it as an extra to an Intent:
val userData = UserData("John", 25) val intent = Intent(this, ReceiverActivity::class.java) intent.putExtra("userData", userData) startActivity(intent)
Receiving Data:
In the receiving activity, you retrieve the Parcelable object from the Intent extras:
val userData = intent.getParcelableExtra<UserData>("userData")
Comparison:
Both Serializable and Parcelable interfaces are used to transfer data between components in Android. However, Parcelable is more efficient than Serializable when it comes to performance. Serializable uses reflection, which can slow down the serialization and deserialization process. On the other hand, Parcelable requires explicit implementation, but it performs better by using direct memory access.
Feature | Parcelable | Serializable |
Performance | Generally faster and more efficient serialization. | Slower serialization, especially for large objects. |
Implementation | Requires explicit implementation of Parcelable methods. | No special methods need to be implemented. |
Boilerplate Code | Requires writing Parcelable implementation boilerplate. | No additional boilerplate code is required. |
Efficiency | Better suited for passing large or complex objects. | Suitable for simpler data structures and objects. |
Usability | More complex to implement due to Parcelable methods. | Simple to implement; just implement Serializable. |
Intent Compatibility | Ideal for passing data via Intents between components. | Can also be used for Intent data exchange, but less efficient. |
Inter-Process Communication (IPC) | The preferred choice for IPC due to performance. | Less efficient for IPC compared to Parcelable. |
Backward Compatibility | No issues with backward compatibility. | Generally backward compatible, but changes in class structure may cause issues. |
Maintenance | Requires updating Parcelable implementation for changes. | Less maintenance overhead as no Parcelable methods need to be updated. |
Parcelable is typically preferred for its better performance, especially when dealing with large or complex objects or when efficiency in inter-process communication (IPC) is crucial. However, Serializable remains a simpler option for simpler data structures or when backward compatibility is a significant concern. As an experienced Android developer, choosing between the two depends on the specific requirements and constraints of your project.
Alright devs, It's time to wrap up this blog. I hope this blog helps you to understand Launch Mode in Android. Ok, then we'll meet on our next blog. In the next blog, we explore WorkManager in Android
Connect with Me:
Hey there! If you enjoyed reading this blog and found it informative, why not connect with me on LinkedIn? π You can also follow my Instagram page for more mobile development-related content. π²π¨βπ» Letβs stay connected, share knowledge and have some fun in the exciting world of app development! π