Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 598
mohan Avatar asked Aug 11 '12 09:08

mohan


People also ask

Why do we need serialization in Android?

Android is following Java syntax. In java we can pass object to another class without serializable. when ever an object's state needs to be saved to retrieve it after some time we need to serialize the object.

Why do we need to serialize object?

Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions such as: Sending the object to a remote application by using a web service.

How pass data from one activity to another activity in Android?

You can pass an ArrayList<E> the same way, if the E type is Serializable . You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval. Example: ArrayList<String> myList = new ArrayList<String>(); intent.

What is the purpose of serializable interface?

Serializable interface. Serializable is a marker interface (has no data member and method). It is used to “mark” java classes so that objects of these classes may get certain capability. Other examples of marker interfaces are:- Cloneable and Remote.


2 Answers

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.

like image 163
code-jaff Avatar answered Oct 14 '22 21:10

code-jaff


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

like image 31
Sagar Avatar answered Oct 14 '22 21:10

Sagar