When Will serialization be used?
1. Permanently Save the object and save the object's byte sequence to a local file.
2. Passing objects in the network by serializing objects
3. Passing objects between processes through serialization
In Android, it can be used to transmit complex types in the activity, mainly to convert its class into a basic byte array. In Android, the activity transmission mainly relies on the intent class, the efficiency of data transmission within the intent class still needs to be copied. If the data is too large, you can use the singleton mode to define a global static class and use the design mode.
Parcelable
Putextra (string name, parcelable value) // pass a parcelable object
Putextra (string name, parcelable [] value) // pass a parcelable Array
Putextra (string name, bundle value) // pass a bundle object
Putparcelablearraylistextra (string name, arraylist <? Extends parcelable> value) // transmits an array of objects that implement the parcelable interface.
Putstringarraylistextra (string name, arraylist <string> value) // transmits a string array.
Putintegerarraylistextra (string name, arraylist <integer> value) // transmits an integer array.
Intent also implements basic array types such as putextra (string name, int [] value), and parcelable is more efficient than standard Java serialization, it is also used to exchange data in aidl. Therefore, the display serialization technology like parcelable can greatly improve the data exchange performance.
Serializable
The traditional Java and serializable technology implement the serializable interface. The principle is similar to that of parceble. They all convert their classes into basic types, such as byte arrays, which is not very practical in Android, this method is less efficient than parcelable. You can use the intent putextra (string name, serializable value) method in intent.
Differences
- When using memory, parcelable has higher performance than serializable. Therefore, parcelable is recommended.
- Serializable will generate a large number of temporary variables during serialization, resulting in frequent GC.
- Parcelable cannot be used when you want to store data on a disk, because parcelable cannot guarantee data continuity in the case of external changes. Although serializable is not recommended for low efficiency, we recommend that you use serializable in this case.
Part of the data comes from