7

I was wondering, why Google makes it so difficult to pass list of Objects around, from 1 activity to another activity, even though my activities are all within a single process?

Why can't they just have something like

intent.putExtra("histories", listOfHistoryObjects);

Are they overly design?

Community
  • 1
  • 1
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

3 Answers3

6

One reason:

  1. Activity A starts activity B, handling it an argument that is a direct pointer to an object.
  2. The user presses home.
  3. Your process is killed while in the background.
  4. The user returns to your app.
  5. Activity B is now re-created.
  6. Ooops, the object argument it was supposed to be running with no longer exists.

Another reason:

Activities are top-level components representing the major parts of your application. The platform manages them for you across processes; whether or not activity A and B are running in the same process, everything you supply to startActivity() must go through IPC to the system process, and the system process maintains the state about that activity and hands it back to your process when activity B needs to be created. It also maintains this across re-launches of your process. It couldn't do any of this if it allowed any arbitrary Java object that couldn't be transported across IPC boundaries.

If you have elements of your UI that are tightly coupled, you can use Fragment to implement the separate parts of your UI. This class is not a top-level part of your application and does not provide any support for use across processes, so makes it easy to have direct interaction between fragments or a fragment and its activity.

That said... this still doesn't remove the need for you to work correctly in the first case. If you aren't going to use the Bundle-based arguments for fragments (which allow the system to automatically take care of keeping that data across process instances), you will need to take care to implement onSaveInstanceState and such to correctly re-create your fragments in their appropriate state.

Also please please do not use Serializable for storing objects into a Parcel or Bundle. As the documentation at http://developer.android.com/reference/android/os/Parcel.html#writeSerializable(java.io.Serializable) says, these is tremendously inefficient compared to implementing Parcelable (or even just using the built-in primitives in a Bundle).

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • +1 for your bullet-point list but I have to disagree when it comes to serializing objects in some situations. It basically comes down to a per-case basis. In some cases the level of inefficiency is negligible and to simply dismiss the approach isn't really justified. – Squonk Mar 16 '12 at 08:09
  • 1
    Putting the contents in a Bundle will probably be more efficient. – hackbod Mar 30 '12 at 05:47
1

As long as you stay in single process, you may just pass objects by reference using standart java techniques. NO need of intents, and singleton container is just fine in android.

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
0

I think you just need to implements Serializable interface in your class.

And after that you can directly write:

intent.putExtra("histories", listOfHistoryObjects);

Check this example for the detailed description.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295