Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42710

Why it is so cumbersome to pass list of objects from one activity to another activity

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?

Upvotes: 7

Views: 492

Answers (3)

hackbod
hackbod

Reputation: 91331

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).

Upvotes: 6

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

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.

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128428

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.

Upvotes: 0

Related Questions