What Is parcel in Android?

Source: Internet
Author: User

Parcel, which means "packaging. What about packaging? It is for serialization.

If you want to pass an integer between processes, it is easy to directly pass the line; if you want to pass a string, it is a little complicated: You need to first allocate a memory that can hold the string, then copy the string to the memory and pass it again. (A newbie may ask: why not directly pass the string reference? Everyone who has learned C/C ++ knows that a process has its own memory address space. The 1000 address in one process may be 100000 in another process, java object reference, local or memory address); what if you want to pass an instance of a class? Is it also possible to allocate memory for the class first, and then copy one copy before passing it? I don't think so. I can find at least one reason: in addition to attributes, the class members also have methods. Even if the attributes can be completely passed in, what about the methods? The method exists independently of the class object, so an error occurs when you reference the same method in another process, or because of the independent address space.

In Android development, data is often transmitted between various activities. In the design architecture of Android, even if the activity in the same program does not necessarily run in the same process, therefore, when processing data transmission, you cannot always assume that both the two activities are running in the same process. Therefore, you can only process data transmitted between processes to make them the most widely adaptable.

So how to transfer class objects between processes? Simply put, we can do this: In process a, pack the non-default attributes of the class and the unique identifier of the class (this is called serialization), and pass the package to process B, after receiving the package, process B creates the class with the unique identifier of the Data class and updates the passed attributes to the class object, in this way, process a and process B contain two identical class objects.

Next, let's talk about parcel in detail, To be continued...

Related Article

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.