Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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?

  • http://androidideasblog.blogspot.com/2010/02/passing-list-of-objects-between.html
  • Help with passing ArrayList and parcelable Activity
  • http://developer.android.com/resources/faq/framework.html#3

Why can't they just have something like

intent.putExtra("histories", listOfHistoryObjects);

Are they overly design?

like image 455
Cheok Yan Cheng Avatar asked Mar 16 '12 07:03

Cheok Yan Cheng


People also ask

How do you pass lists between activities?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How do you send an ArrayList to another activity?

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.


2 Answers

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

like image 116
hackbod Avatar answered Sep 18 '22 01:09

hackbod


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.

like image 24
Konstantin Pribluda Avatar answered Sep 20 '22 01:09

Konstantin Pribluda