0

I saw this post here in Stackoverflow. If I want to pass an object to another activity, the class should be serializable. So my question, Is it possible to implement serializable to a Pojo class? Is there any conflicts if I will implement Serializable or Parcelable?

AJ Seraspi
  • 113
  • 1
  • 14

3 Answers3

2

As long as every member of your POJO also implements Serializable, you should be fine.

There will be conflicts only if your POJO has a child member which is another POJO and that POJO does not implement Serializable.

Members like String, int, enums etc. are Serializable by default, so you should only be concerned if there is a non trivial property that does not implement Serializable.

entropy.maximum
  • 477
  • 4
  • 16
1

Yes it is possible to implement Serializable or Parcelable to a POJO class. First of all, a small introduction: Serializable is a standard Java interface. It is using reflection and it is a slow process. This method create a lot of temporary objects and cause quite a bit of garbage collection. Parcelable process is much faster than serializable. It is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.

1

So my question, Is it possible to implement serializable to a Pojo class?

Yes it is. Just add to your POJO class that implements Serializable and you class will be serializable. So if you need to pass objects between activities, it's mandatory for your classes and subclasses to be serializable.

public class ModelClass implements Serializable {}

Is there any conflicts if I will implement Serializable or Parcelable?

No, there will be no conflicts at all but there are some differences in using one or the other interface that you should take care of. So for that I recommend you see the following posts: Parcelable vs Serializable.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193