Storage and reading summary and simple use of Android serialization

Android serialization

1. Purpose of serialization

(1) . permanently save the object data (save the object data in a file or disk)

(2) . transmit the object data on the network through serialization operation (because the network transmission transmits the data in the form of byte stream, the purpose of serialization is to convert the object data into the form of byte stream)

(3) . transfer the object data between processes (when transferring object data between activities, you need to serialize the object data in the current activity. You need to deserialize the object data in another activity to get the data)

(4) The. Java platform allows us to create reusable Java objects in memory, but generally, these objects can exist only when the JVM is running, that is, the life cycle of these objects will not be longer than that of the JVM (that is, each object is in the JVM). However, in real applications, the JVM may be stopped, but some specified objects need to be saved, And reread the saved object in the future. This is java object serialization to achieve this function. (you can choose to save in the form of database or file)

(5) . when serializing objects, only variables are serialized, not methods

(6) Between intents, basic data types can be passed directly, but serialization is required once the data types are complex

There are two ways to implement serialization in Android: serializable interface and Parcelable interface. This paper briefly summarizes and uses these two ways.

1、 Related concepts

(1) Reason for serialization (effect of serialization)

1. Permanently save the object and save the byte sequence of the object to the local file;

2. Objects are transferred in the network; 3. Objects are transferred between IPC.

(2) Serialization method

In the Android system, there are generally two methods of serialization, namely, implementing the serializable interface and the Parcelable interface. The serializable interface is the serialization interface from Java, and the Parcelable is the serialization interface of Android. The above two serialization interfaces have their own advantages and disadvantages, which should be determined according to different situations in actual use.

1. Use the Parcelable interface when more memory is required.

Serializable generates a large number of temporary variables during serialization, resulting in frequent GC. In contrast, the performance of parallelable is higher (after all, it comes with Android). Therefore, when using memory (such as serializing objects to transfer objects in the network or serializing objects between processes), it is more recommended to use the parallelable interface.

2. When local storage is required, use the serializable interface.

However, Parcelable has an obvious disadvantage: it cannot be used when the data is to be stored on disk (for example, permanently save the object and save the byte sequence of the object to the local file), because Parcelable is essentially a general serialization mechanism for better object transmission between IPC, When changing the underlying implementation of any data in parcel, the previous data may not be readable, so it is recommended to use serializable at this time.

2、 Use of serializable interface

The interface implementation of serializable is very simple. You only need to make the class to be serialized inherit serializable, and the system will serialize it automatically. When storing, use fileoutputstream to construct an objectoutputstream, and use writeobject to store objects. When reading, use FileInputStream to construct an objectinputstream, and use readObject to read objects.

(1) Layout file activity_ Design of main.xml

Interface design: input data through several input boxes, two buttons, one to save data and one to read data. The read data is displayed under a text box.

(2) Create a property class that inherits serializable

(3) Class of main method

Here, the external storage method is used to store data. You need to add permissions:

Interface after program operation:

Enter the corresponding information, click save, and then click read to read the displayed results:

The data here is saved in the local file. The last written file can be read directly without writing data next time.

3、 Use of Parcelable interface

The process of using the method should be more troublesome!

The implementation of the Parcelable interface can be divided into the following steps:

1. Let the attribute class model implement the Parcelable interface. 2. Rewrite the writetoparcel method to serialize your object into a parcel object,

That is, write the data of the class into the externally provided parcel, package the data to be transferred to the parcel container and save it, so as to obtain the data from the parcel container. The file writing method here is very important.

3. Rewrite the describecontents method to the content interface description, and return 0 by default. This method is basically useless! 4. Instantiate the static internal object creator, implement the interface parcelable.creator, and rewrite the read abstract method.

The method of reading here is also very important. It must be consistent with the order of writing. The name of the creator interface object here is fixed. If you change it to another name, the bottom layer will not recognize this interface!

Note: if parcel is regarded as a stream, first write the object to the stream through writetoparcel, and then read the object from the stream through createfromparcel. Therefore, the write order and read order of the class implementation must be consistent.

Here, the design program jumps from one page to another and passes the data of the object.

(1) The design property class inherits the Parcelable interface

(2) Class design of main method

The above class is also very simple. Design a button to listen and jump to another page.

(3) Design of another page

The above page is also relatively simple. It receives the objects passed from the previous page and displays them in a textview.

Display interface after program operation:

Click the big button to display the interface:

The above data is written dead. In fact, you can use several input boxes to determine the data like the first program.

Compare the methods and effects of the two interfaces:

For the first program, serializable is used to transfer data, and the data is saved locally. Even if the program is unloaded, other programs can access the data of the saved file as long as the file path is correct. It can also be used for inter process communication, but it needs to consume some memory.

Compared with the second program, parcalable is used to realize data transmission. The data here cannot be saved locally and occupies less memory. It is more suitable for data transmission between processes. For application: network information transmission and inter process data transmission, there are a little more ways to realize data transmission using parcalable.

For these two kinds of data, the size of information transmitted can not be very large.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>