32

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;
    }
}
kenju
  • 5,248
  • 1
  • 36
  • 40
SRam
  • 2,704
  • 4
  • 41
  • 70
  • 5
    Did you go through these - http://stackoverflow.com/questions/3323074/android-difference-between-parcelable-and-serializable and http://stackoverflow.com/questions/5550670/benefit-of-using-parcelable-instead-of-serializing-object ? – verisimilitude Jun 18 '12 at 06:36
  • 3
    Use capital letters to start off a class name (_GetSetClass_ or _Getsetclass_). – keyser Jun 18 '12 at 06:38
  • Whats the need of serializing of objects in android? may we don't perform the task without serializing the objects? – SRam Jun 18 '12 at 06:41

5 Answers5

28

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.

Brad Larson
  • 168,330
  • 45
  • 388
  • 563
user1923551
  • 4,396
  • 32
  • 28
  • 4
    I have no idea why this answer hasn't got any upvote until now, it's quite complete and to be honest, one of the bests for me. – GoRoS Apr 15 '14 at 20:52
  • @vsvankhede Well, it seems there is a time and place for everything and currently it's the most upvoted one. – GoRoS Oct 30 '15 at 09:42
19

whether should i used parcelable or serialization technique for sending data from one activity to other.

If you are sending a non-primitive type data/Object to another activity through the intent you have to either Serialize or implement Parcelable for that object. The preferred technique is Parcelable since it doesn't impact the performance.

is it compulsory to use one of them for sending data from one to other. / when should i use them.

It is only compulsory/used for sending non-primitive type data objects.

and the exact difference between them and performance of both of them in java aspects.

Serialization does impact the performance. For more details check this link Android Parcelable and Serializable

Community
  • 1
  • 1
Arun George
  • 17,272
  • 4
  • 25
  • 28
  • @hi arun George ,..why should i use serialization for only non-primitive data type ? and why it is compulsory for non- primitive dat type only? – SRam Jun 18 '12 at 07:22
  • 2
    Primitive data types can be passed from one activity to another using `intent.putExtras(String id, PRIMITIVE_DATA_TYPE)` and accessed in the other other activity using the getMethod() equivalent for that type. But such is not the case for user defined data types and thus you have to use either `serialization` or `Parcelable`. – Arun George Jun 18 '12 at 07:27
  • 1
    ok thanks for answering one more question ..when should we use Bundle for sending data and whats the need to put object in Bundle intent.putExtras(String id, PRIMITIVE_DATA_TYPE) if we r using serialization for sending data.. – SRam Jun 18 '12 at 07:35
5

Got a very good explanation of difference between Parcelable and Serialization.

To start with your question though its been a long time, it may help others:

  1. whether should I used parcelable or serialization technique for sending data from one activity to other?

Ans: Parcelable is best choice (why explained later).

  1. is it compulsory to use one of them for sending data from one to other?

Ans: Yes, as sending data (object) from one to other requires streams of bytes to be written and retrieved and that can be done either through parcelable or serialization.

  1. when should I use them?

Ans: This part you arleady answered i.e, passing data from one activity to another.

  1. and the exact difference between them and performance of both of them in java aspects.

Ans: 1. Parcelable is well documented in the Android SDK; serialization on the other hand is available in Java.

  1. In Parcelable, developers write custom code for marshaling and unmarshaling so it creates less garbage objects in comparison to Serialization. The performance of Parcelable over Serialization dramatically improves (around two times faster), because of this custom implementation.

  2. Serialization is a marker interface, which implies the user cannot marshal the data according to their requirements. In Serialization, a marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API. This helps identify the Java objects member and behavior, but also ends up creating a lot of garbage objects. Due to this, the Serialization process is slow in comparison to Parcelable.

Answer taken from: this link

See also:serialization explained

Community
  • 1
  • 1
Bette Devine
  • 1,188
  • 1
  • 8
  • 23
3

Java Serializable:- Serializable comes from standard Java and is much easier to implement all you need to do is implement the Serializable interface and add override two methods.
The problem with Serializable is that it tries to appropriately handle everything under the sun and uses a lot reflection to make determine the types that are being serialized. So it becomes a beefy Object.

Androids Parcelable:- Android Inter-Process Communication (AIPC) file to tell Android how is should marshal and unmarshal your object.It is less generic and doesn't use reflection so it should have much less overhead and be a lot faster.

Read More from http://www.3pillarglobal.com/blog/parcelable-vs-java-serialization-android-app-development

Sridhar Nalam
  • 491
  • 8
  • 8
2

both parceling and serializing are ways to marshall and unmarshal data. in android this is used to pass non-primitive data types between components and processes. in general, android allows either serializable or parcelable objects, so you can choose your method. the exception to that is with AIDL interfaces. objects must be parcelable to be passed / returned.

serialization uses reflection to automatically marshall and unmarshal data. in most cases implementing the marker interface is enough to make it just work. with parceling, you have to write the code to marshall and unmarshal the data yourself.

and hence, that is why parceling is faster. the object does not need to be reflected to discover the fields. it's the reflection that makes it slow.

serialization also has in-built versioning ... if you try to unmarshal to a different version of the object's class that was marshalled, the process will fail in a predictable way. with parceling, you can do the same thing but you need to implement it yourself by adding a "version field to your object, and code that checks the version when unmarhsaling.

that being said, i typically use serialization. with simple objects you won't notice the difference. you can always change to use parceling later in development if you discover performance issues.

Jeffrey Blattman
  • 21,054
  • 8
  • 74
  • 127