转自: http://www.stevenmarkford.com/passing-objects-between-android-activities/
Introduction
It is sometimes convenient to pass through a java object (POJO) from one activity to another as opposed to sending multiple separate primitives via bundle extras.
We will be looking at three different methods of passing objects between activities:
- The standard Android approach using the Android Parcelable class
- Using the standard Java Serializable interface
- Using a light weight (~42Kb) Android event bus library called EventBus
After trying these approaches I prefer using an event bus.
In the examples below we will assume we want to send the following POJO from one activity to another:
Example 1: Using Standard Android Parcelable
Step 1 Extend our POJO with the Android Parcelable base class and implement required methods…Sounds easy right? Brace yourself…the result is a mutant POJO with IBS that just ate a large loaf of white bread!
That POJO does not feel very Coca-Cola anymore!
Step 2 We send the POJO off to our destination activity
Step 3 Our destination activity reads the result
Example 2: Using Java Serializable
Step 1 Update the POJO to extend the serializable interface. The POJO doesn’t change much when using Serializable:
Step 2 We send the POJO off to our destination activity:
Step 3 Our destination activity reads the result:
Example 3: Using EventBus
To setup EventBus simply download the jar from here and reference it in your Android Project (see Eclipse tutorial on adding a jar to a project here), or if you using Maven then the details are: group ID “de.greenrobot” and artifact ID “eventbus”.
Step 1 We pop unmodified POJO, as is, onto the bus (no need for all that boilerplate code):
postSticky() simply persists the object on the bus and allows it to be picked up later by the destination activity.
Step 2 Our destination activity pops the POJO off the bus:
We tell the Bus to give us the last posted object of type POJO.
Notice how you no longer need magic strings to manage the key values of the bundle. You use the actual .class, which
essentially strongly-types your references.
Efficiency Comparison
Efficiency is always important, not only can little inefficiencies scattered around the code add up to large noticeable ones but overtime it can result in battery-hog.
Below is a time vs communication count chart comparing the different methods using the POJO object:
There is not really any difference in performance for a simple POJO that only contains four fields.
Now lets pass around the following object and see what happens:
From the above chart you can see EventBus does not depend on the complexity of the object where as the other two approaches do.
Now for abit more fun lets look at what happens when MammaPOJO has 1000 babies.
Conclusion
EventBus seems like a pretty good option: It requires a lot! less boilerplate, complements an agile coding-style, is more strongly typed (requiring less hard-coded string management) and it is a bit less of a battery-hog when it comes to complex objects.