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...