Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between Parcelable and Serialization used in android

I want to know exact ,

  1. whether should I used parcelable or serialization technique for sending data from one activity to other?
  2. is it compulsory to use one of them for sending data from one to other?
  3. when should I use them?
  4. and the exact difference between them and performance of both of them in java aspects.

Thanks in advance.


 public class GetSetClass implements Serializable {     private int dt = 10;      /** pass any object, drwabale */     public int getDt() {         return dt;     }      public void setDt(int dt) {         this.dt = dt;     } } 
like image 471
SRam Avatar asked Jun 18 '12 06:06

SRam


People also ask

What is serializable and why Android uses Parcelable?

Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations. Parcelable is an Android specific interface where you implement the serialization yourself.

Why is Parcelable better than serializable?

Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn't create more temp objects while passing the data between two activities.

What is serialization and Deserialization in Android?

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.

What is a Parcelable in Android?

A Parcelable 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 Answers

These concepts are related to Inter Process Communication (IPC).

When sending data between two applications, we have to make sure that both applications should understand the format of the data that is being sent.

Especially when you are sending non primitive data type values like classes and objects between two applications, We have to convert them into Operating System understandable format. O.S understands only primitive types (ints, chars etc). The reason for conversion is we have to O.S communication channel to transmit the data.

This process of converting Non primitive types to primitives and sending across to other application over some communication channel is called as Serialization. The reverse process is called as De Serialization.

In Java, IPC depends heavily on Serializables for serialization. But serialization is designed by keep desktop applications in mind. When you are doing IPC in mobile applications we have to make sure that the process of IPC is not too heavy.

In simple terms serialization is a heavy concept for IPC. So in place of Serialization Android opted for Binders for achieving light weight Inter process communication. Binders internally depends heavily on parcels, to do the IPC. Parcels are light weight serializables. It is preferred to use parcels for marshaling objects into byte streams.

Note: Binder IPC heavily depends on Shared memory concept to make sure that there is not much data duplication while sharing between applications.

like image 144
user1923551 Avatar answered Oct 21 '22 12:10

user1923551