Why we need to serializable object for passing one activity to another activity in Android

Viewed 3495

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

4 Answers

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

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.

Related