What is Serializable and Parcelable?

  • Thread starter Ashish Bisht
  • Start date
In summary, the conversation discusses the two approaches for passing data between activities in Android - Serializable and Parcelable. Serializable is slower and creates temporary objects, while Parcelable is faster but requires more boilerplate code. The decision between the two depends on the specific case and whether the extra time spent on implementing Parcelable is worth it. It is recommended to refer to the Android documentation for a better understanding of the two options.
  • #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 ?
 
Technology news on Phys.org
  • #2
Serializable is a marker interface, that you implement on a class and its children. The downside of this approach is that it's slow and creates a lot of temporary objects.

Parcelable is a more specialized interface, which is significantly faster than Serializable. The downside is that you have to write some - enough in many cases, boilerplate code.

Everything has its cost. If you can afford the time , then it's better to use Parcelable. On the other hand, in most cases the slowness of Serializable - that is due to reflection that is used, won't be particularly noticeable, so you can write significantly less code and be OK. In general, you have to judge yourself on a per case basis, if it worths the extra time or not.

I strongly recommend to look up Android documentation, in order to have a complete view about the two choices.
 
Last edited:
  • Like
Likes harborsparrow and Ashish Bisht
  • #3
Hello Ashish, :welcome:

+1 for QQ. But I wonder why -- as an android aficionado -- you didn't simply google 'serializable vs parcelable' and ended up with D Phil ?
 
  • Like
Likes Ashish Bisht
  • #4
Thank you everyone for valuable suggestion.
 

1. What is the difference between Serializable and Parcelable?

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.

2. Which interface should I use to pass data between Activities in Android?

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.

3. How do I implement Parcelable in my class?

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.

4. Can I use Serializable or Parcelable with custom objects?

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.

5. What are the advantages of using Parcelable over Serializable?

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.

Similar threads

  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
1
Views
971
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
3
Views
648
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top