25

Can anybody please tell why we need to serializable object for passing one activity to another activity in android? Android is following Java syntax. In java we can pass object to another class without serializable.

Thanks

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
mohan
  • 13,035
  • 29
  • 108
  • 178
  • when ever an object's state needs to be saved to retrieve it after some time we need to serialize the object. – J.K Aug 11 '12 at 10:04
  • There is not a real difference between general Java serialization and object serialization with Android. Therefore I would recommend the Oracle tutorial:http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/serial.html – J.K Aug 11 '12 at 10:12
  • All the answers here are eye opening and well put. I've had this question in several situations and read similar questions. IMO, it is a limitation caused by the high level abstraction of `Intent` that certainly need a solution. In most cases, we do not need marshaling rather simple delegation of tasks. I've created a wrapper `TrackedReference` that is parcelable and serializable without requiring marshaling for the underlying type: https://stackoverflow.com/a/64944753/3405387 – Lukas Nov 21 '20 at 16:14

4 Answers4

14

In ordinary java programs passing parameters(Object type), is kind of create a new handler to the object and giving to another method (In regular words passing the reference by value).

But when it comes in android, passing object references from activity to activity, where their states have to be persisted, is a serious headache.

One way you can do is create a static object in the first activity and access from the second, though this seems to be a easiest way, there is no guarantee that the system maintains the activity in the memory. Therefore the second activity may loose the object reference.

Other way, and the mostly recommended way is serializing(Kind of flatten the object) the object and pass with the intent as extra. In android there are two ways to serialize.

  1. Implement the java's serializable interface
  2. Implement the android's parcelable interface

However, on the android, there is a serious performance hit that comes with using serializable, the solution is using parcelable.

You can find a pretty good tutorial and explanation on android parcelable implementation here.

code-jaff
  • 9,230
  • 4
  • 35
  • 56
  • Ok, since the object reference is passed by value, there might be chances that data be cleared by android system in the previous activity. So, the object is written into a stream thus resulting in marshaling and un-marshaling. – Chaitanya Mar 20 '13 at 09:46
  • 1
    If the object is kept in a static field, it's going to be kept in memory, no matter if one instance of the first activity is destroyed or not. Hence it would work, actually does, and is as a result a source of "memory leak". – rds May 10 '18 at 07:47
14

We need to understand following concepts before getting to the answer:

  • Android uses Binder for inter-process process. It is required even for simple app because the OS and the apps run in different processes.
  • Marshalling: A procedure for converting higher level application data structures into parcels for purpose of embedding into Binder transaction
  • Unmarshalling A procedure for reconstructing higher-level application data-structures from parcels received though binder transactions.
  • You can consider Intents as higher level abstraction of Binder

Based on the documentation following is the way how intent communication occurs: enter image description here

  1. Activity A creates an Intent with an action description and passes it to startActivity().

  2. The Android System searches all apps for an intent filter that matches the intent. When a match is found,

  3. the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.

Why Parcelable or Serializable

IPC (Inter Process Communication) requires data in Intent to be Marshalled and unMarshalled. Binder provides built-in support for marshalling many common data-types. However when we define custom object, it would impact this process and the final object received might be corrupted during the process.

When you define custom object, you need to be responsible for providing this marshalling and unmarshalling which is achieved through Parcelable and Serializable (Since comparison between these two would be another topic I won't discuss much here). Both of these provide mechanisms to perform marshalling and unmarshalling. This is the reason why you need to use Parcelable or Serializable.

Using Parcelable you write the custom code for marshalling and unmarshalling the object thereby you gain complete control over the process.

Serializable is a marker interface, which implies the user cannot marshall the data according to their requirements and its done on JVM, which doesn't give any control at your side.

Disclaimer: Description above is my understanding for the rationale behind the need for serialization based on some documentation

Sagar
  • 23,903
  • 4
  • 62
  • 62
3

There are basically two questions in your question, so let's decouple it.

Why marshall in a Parcelable instead of passing an object reference directly?

It's obvious faster and more memory efficient to reference objects rather than marshall/unmarshall them. So you shouldn't use Parcelable when you can pass the object directly.

However, there are situations where you may not have access to the object reference.

  • in Intent because the process that handles the Intent may not be the process that emitted the Intent (it's an inter-process communication)
  • in Activity lifecycle, for instance in onRestoreState(), because the whole app may have been killed by memkiller when the user wants to resume it.
  • everywhere else where Android frameworks requires

In IPC, why use Parcelable rather than Serializable like Java does?

That's only a performance optimization.

rds
  • 26,253
  • 19
  • 107
  • 134
2

If We want to pass object from Activity to to Another Activity . We need to save the passing state.

//to pass :
   intent.putExtra("MyClass", obj);  

// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37