- #1
Ashish Bisht
- 2
- 0
Hi everyone, i am an Android developer and want to know your views on Serializable and Parcelable. Which is best approach in Android that will be helpful for me ?
Serializable and Parcelable are both interfaces in Java that allow objects to be converted into a stream of bytes for transfer or storage. The main difference between the two is that Serializable is a marker interface, while Parcelable requires explicit implementation.
Parcelable is recommended for passing data between Activities in Android because it is more efficient and faster than Serializable. However, if you are passing data between different platforms or over a network, Serializable may be a better choice.
To implement Parcelable in your class, you need to implement the Parcelable interface and override the writeToParcel() method, which defines how your object will be written into a Parcel. You also need to provide a public static final CREATOR field, which is responsible for creating instances of your Parcelable class from a Parcel.
Yes, both Serializable and Parcelable can be used with custom objects. However, Parcelable requires more effort as you need to implement the interface and provide the necessary methods. Serializable is easier to use, but it may not be as efficient as Parcelable.
The main advantage of using Parcelable over Serializable is performance. Parcelable is optimized for Android and is more efficient in terms of time and memory usage. It also allows for more control over how your data is written and read, making it a better choice for passing data between Activities in Android.