When developing Android, we sometimes need to use persistent storage of data or transfer data between processes. This may require object serialization, which can be transmitted through Intent or Boundle. Let's introduce it in detail. 1. What is serialization and what is deserialization Serialization: The process of converting a data structure or object into a binary string. Deserialization: The process of converting the binary string generated during the serialization process into a data structure or object. In simple terms, serialization is to store the objects we generate (such as on disk) for future use or transmission on the network, while deserialization is the process of regenerating objects from the binary strings generated by our previous serialization. Note that the serialization and deserialization we repeatedly mention here are all for objects, not classes. Because we access and transmit objects, not classes, when we need to retrieve the previous object, we read it directly (from a file or network), rather than creating a new object based on the class. This is something that needs to be noted. 2. How to serialize There are two ways to serialize, one is to implement the Serializable interface, the other is to implement the Parceable interface, these two methods will be described in detail below. a. Implement the Serializable interface This serialization method is provided by Java. Its advantage is simplicity. In fact, the Serializable interface is an empty interface, so we do not need to implement any abstract methods. However, we often need to declare a static variable identifier (serialVersionUID) in the class, but this is not necessary. We can still implement serialization without declaring it, but this will have a certain impact on deserialization, and may cause the deserialization of the object to fail after we modify the class. The declaration method is as follows:
Note that the value here can be anything. Let's implement it in detail below.
The following is the serialization and deserialization process:
Screenshot of the operation results: Note: If you call the above method in an Android project, don't forget to configure the following permissions in the Manifest.xml file:
b. Implement the Parceable interface This method is provided by Android. Compared with the previous method, this method is a little more complicated. We need to perform serialization and deserialization operations by ourselves, but it is more efficient and does not require a large number of I/O operations. And this method is also the serialization method recommended by Android, so we should use Parceable. As long as this interface is implemented, objects of a class can be serialized and passed through Intent and Binder. Please see the example below:
Here, the Book class implements the Parcelable interface. In fact, in Android Studio IDE, the above process is very simple. We only need to define a class, implement the Parcelable interface, and then define our attributes or fields in it. According to the prompt error, overwrite the corresponding method according to the prompt method. Everything after that can actually be automatically generated (but if you need to construct a method, you need to generate it automatically, and the toString() method is also implemented by yourself), so don’t worry about it being troublesome to implement the Parceable interface in Android development, because AS will automatically generate it for you. Above, we have completely implemented the Parceable interface for the Book class, so how to serialize and deserialize it next? If you say, didn’t I just say that it will be possible to use file reading... When you do that, you will find that the following error will be reported: Why???...What's going on? It reminds us that the Book class is not serialized:
Well, the reason for this problem is not that there is a problem with our implementation process, but that the way of using this class does not work. At this point, we understand that there is a difference between Serializable and Parceable in implementing serialization. As we said just now, Parceable is more efficient and does not have a large number of I/O operations like Serializable. The specific meaning of this sentence reveals the difference between Serializable and Parcelable: Although both are used to support serialization and deserialization operations, the biggest difference between the two lies in the different storage media. Serializable stores serialized objects on the hard disk and uses I/O reading and writing, while Parcelable stores them in memory and reads and writes memory. Friends who are familiar with the principles of computer composition know that the reading and writing speed of memory is obviously much faster than the reading and writing speed of I/O. This is why Android recommends using Parcelable to implement object serialization. So how should we use objects that are serialized by implementing the Parcelable interface? The answer is: through the Intent method. In addition to basic types, Intent can only transmit serialized objects. Corresponding to these two serialization methods, there are also two corresponding methods:
Of course, there is no such distinction for insert operations, they are all methods:
We can put the serialized object into the Intent in the first Activity and take it out in another Activity, for example: Get the object at the other end, for example:
Next, let's look at the process of implementing the Parceable interface in the User class. It contains a serializable class Book. The specific details are slightly different from the above:
As you can see, the result has been printed correctly: Note: In Parcelable, we cannot write boolean values directly, but convert them into integer values for storage, here is Byte, of course, you can also use Int, etc. |
>>: Rexxar: Douban's thoughts on hybrid development
In terms of promotion and traffic , different pro...
“Can orthokeratology lenses really delay the prog...
As the temperature gradually drops, the ginkgo be...
In fact, most of the Xiaohongshu merchants are mo...
Baidu Netdisk Super 0 Tutorial Course Materials 1...
How to correctly direct traffic during the promot...
Today I want to share with you a blue ocean proje...
1. Conditions for registering a partnership 1. Th...
Luo Yonghao was slapped in the face again, and th...
Baidu Union Promotion is one of the core products...
In today's mobile Internet era, we receive a ...
What is the core competitiveness of event plannin...
Have you ever encountered the traps of holiday tr...
On November 6, DeepBlue Automotive's intellig...
1. Industry Background As we all know, Douyin is ...