Intent passed object -- difference between Serializable and Parcelable, intentparcelable

Source: Internet
Author: User

Intent passed object -- difference between Serializable and Parcelable, intentparcelable

The first two articles discussed how to transmit objects and object arrays between Serializable and Parcelable Intent. The two methods are similar in implementation and the effects are consistent. How can we choose which method to implement them?

Intent is widely used to transmit object data among different components. The following describes two methods for passing objects through Intent.

1. Implement the Serializable Interface

2. Implement the Parcelable Interface

 

Why serialize objects?

1. Permanently Save the object and save the object's byte sequence to a local file;

2. Passing objects in the network using serialized objects;

3. Pass objects between processes by serializing objects.

 

1. Implement the Serializable Interface

Serializable is used to store data objects in byte streams and regenerate objects as needed. Its main application is to use external storage devices to save object statuses and transmit objects over the network.

The implements Serializable interface is used to mark an object and the system will automatically serialize it.

2. Implement the Parcelable Interface

1) Why do we need to implement the Parfcelable interface to implement object transfer in Intent?

A. When using memory, Parcelable has higher performance than Serializable. Therefore, Parcelable class is recommended.

B. Serializable will generate a large number of temporary variables during serialization, resulting in frequent GC.

Note: Parcelable cannot be used to store data on disks, because Parcelable cannot store data continuously in the case of external changes. Therefore, Serializable is recommended in this case.

2) New serialization mechanism in Android

For mobile devices with limited memory in the Android system, the performance requirement is higher. The Android system adopts a new IPC (inter-process communication) mechanism, an object transmission method with better performance is required. ThereforeParcelClass is designed, which is positioned as a lightweight and efficient object serialization and deserialization mechanism.

The serialization and deserialization of Parcel are all performed in the memory, so the efficiency is much higher than that of using external memory in JAVA serialization.

 

Parcel class

For applications, the Parcel class is often used to transmit data between activities. When Intent is used between activities to transmit data, you can use Parcelable to transmit complex objects.

Parcel mechanism: essentially, it can be regarded as a Serialize. Only the Parcel object is serialized and deserialized in the memory, and uses the continuous memory space, so it is more efficient.

Like a friend, click like! Thank you.

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.