Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing object for sending inside Intent on Android

I want to pass objects between Activities and Services on Android. The straight forward way to do this is to make your objects implement Serializable or Parcelable.

  • Serializable it's relatively bad performance.
  • Parcelable on the other hand requires me to implement and maintain the serialization myself i.e. always to remember to update it in the future.

I was thinking about using Jackson Json serializer for this task. It's faster than Java built in serialization and does not require me write and maintain serializtion code.

What you think?

like image 683
AlexV Avatar asked Oct 11 '22 18:10

AlexV


2 Answers

If performance is important, then you really should go with Parcelable. JSON will be relatively slow because it is less efficient than Parcelable which was specifically implemented in Android for improving performance when packaging data for Inter-Process Communication.

like image 98
Mark Allison Avatar answered Oct 14 '22 01:10

Mark Allison


I believe both the methods have its own importance.

  • Parcelable is a good choice. But using parcelable you will have to write code for serialization yourself. This method is not encouraged when you are having large number of data members in your class, whose object you want to send.
  • On the other hand serialization is for sure bulky operation but I think easy to implement. You will have to make your class implements serializable.

But still there is one more option, which you can use. You can make your object static and access it from other Activity. if object is not a lot bulky, then this is also good choice.

like image 24
N-JOY Avatar answered Oct 14 '22 01:10

N-JOY