Android – Intent and Parcelable object Android

androidandroid-intentparcelable

Why do I need to parcel my object even if I just need to send it to another thread of the same task? Actually I need to open an activity that will run even on the same thread (the main thread).

In other words why didn't Google provide a version of startActivity that take a generic object ad parameteer instead of a bundle to let me start an activity in case I know it is within the same process or (most of the times) even the same thread (the main one)?

Best Answer

You don't need to use Parcelable to pass an object from one activity to another. You can just store a reference to the object in a static member variable, like this:

public class Globals {
     public static MyObject myObject;
}

Now, in the code that has the object, you just do:

Globals.myObject = object;

and in the new activity, you can get it like this:

doSomethingWith(Globals.myObject);

Now, having said that, you need to be aware of the following:

Android can kill your process if your application is in the background pretty much any time it wants to. When the user then returns to your application, Android will create a new process for your application and then it will recreate only the activity that was on the top of the activity stack (ie: the one that was showing). In that case, the newly created activity will not be able to get the iobject by accesing Globals.myObject because the process has been newly created and that member variable is null.

To get around this you can either:

  1. Determine that your process has been killed and restarted (by checking Globals.myObject == null and react accordingly - Tell the user he needs to go back, or just go back yourself, or show a dialog or whatever)
  2. Save the object when Android calls onSaveInstanceState() in your activity (which Android will do before sending your app to the background) and restore the object in onCreate()

Hopefully this both answers your question and explains what to do about it.

EDIT: Add more information about why Intents contain serialized (Parcelable) objects and not the objects themselves

  1. When you call startActivity() or startService() Android may end up starting the activity or service in another process. In this case, if you passed an object in the Intent, Android would somehow need to serialize that object to pass it to the other process. Because of the "implicit Intent resolution" that Android uses to determine which component gets to handle the Intent, the caller may or may not know which component will get started.

  2. Android saves the contents of Intents for various reasons:

    A. Android can kill a process at any time. If it does that and the user wants to return to the application, Android creates a new process and then recreates the activities in that process as needed. To create the activities Android also needs to make the Intents available to the activities. If the process has been killed then any "objects" in the Intents would have to be saved and restored. Because the Intents contain serialized objects, it isn't a problem to recreate these as needed. B. PendingIntents are use by Android as a way for the Operating System to act as a proxy for the sender of an Intent. An Android component can create a PendingIntent and give that to the Operating System so that it can trigger the sending of that Intent at some later time. The sending component may or may not be active at the time that the PendingIntent is actually sent. This means that any object that could be passed in a PendingIntent must be able to be serialized so that Android can hold on to it even if the calling component no longer exists.

  3. Intents are not intended as a general "parameter passing" mechanism between components. Of course you can use it like that, but you can also use other (easier) mechanisms. Within a given process you can pass objects around using standard Java mechanisms. There is nothing wrong with using static (class) variables for this.