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).
hackbod
source share