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

I was wondering why Google makes it so difficult to pass a list of objects around, from one action to another, although my actions are all in one process?

Why can't they just be something like

intent.putExtra("histories", listOfHistoryObjects); 

Are they overly design?

+7
source share
3 answers

One of the reasons:

  • Action A begins action B by processing it with an argument, which is a direct pointer to an object.
  • User clicks home.
  • Your process is killed in the background.
  • The user returns to your application.
  • Action B is now recreated.
  • Ooops, the argument of the object that was supposed to work no longer exists.

Another reason:

Activities are top-level components that represent the main parts of your application. The platform manages them for you through processes; regardless of whether operations A and B work in the same process, everything you provide startActivity () must go through the IPC to the system process, and the system process maintains the state of this action and passes it back to your process when activity B must be created. It also supports this when you restart your process. He could not have done any of this if he had allowed any arbitrary Java object not to be transported across IPC boundaries.

If you have elements of your interface that are closely related, you can use Fragment to implement individual parts of the user interface. This class is not part of the top level of your application and does not provide support for use through processes, therefore, it simplifies the direct interaction of fragments or fragment and its activity.

However ... this still does not eliminate the need for proper operation in the first case. If you are not going to use Bundle-based arguments for fragments (which allow the system to automatically take care of storing this data through process instances), you will need to take care of implementing onSaveInstanceState and thus correctly recreate fragments in the appropriate state.

Also, please do not use Serializable to store objects in a package or package. As the documentation says at http://developer.android.com/reference/android/os/Parcel.html#writeSerializable(java.io.Serializable) , this is extremely inefficient compared to the Parcelable implementation (or even using the built-in primitives in Bundle).

+6
source

As long as you remain in a solitary process, you can simply pass objects by reference using standard java methods. NO intent, and the singleton container is great for Android.

+1
source

It seems to me that you just need to implement the Serializable interface in your class.

And after that you can directly write:

 intent.putExtra("histories", listOfHistoryObjects); 

For a detailed description of this example.

0
source

All Articles