Serializable and parcelable serialization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.