Why
Why should I understand serialization? --When doing Android development, you cannot pass the object reference to activities or fragments, we need to put these objects into a intent or bundle, and then pass them on.
What
What is serialization-serialization, which represents the transformation of an object into a state that can be stored or transmitted. The serialized object can be transferred on the network or stored locally.
How
How do I transfer objects by serialization?
Android Intent if you want to pass a class object, you can do it in two ways.
Mode one: Serializable, the class to be passed implements the Serializable interface pass object,
Mode two: Parcelable, the class to be passed implements the Parcelable interface to pass the object.
Serializable (Java comes with):
Serializable is the meaning of serialization, which indicates that an object is converted to a state that can be stored or transportable. The serialized object can be transferred on the network or stored locally.
Parcelable (Android only):
In addition to serializable, using parcelable can also achieve the same effect,
However, unlike serializing an object, the implementation of the Parcelable method is to decompose a complete object,
Each part of the decomposition is a data type supported by intent, which also implements the function of passing objects.
Realizing the role of parcelable
1) Permanently save the object, save the object's byte sequence to the local file;
2) passing objects through a serialized object in the network;
3) Passing objects between processes through serialization.
Principles for choosing a serialization method
1) When using memory, parcelable performance is higher than serializable, so we recommend using Parcelable.
2) serializable generates a large number of temporary variables at serialization time, resulting in frequent GC.
3) parcelable can not be used in the case of storing data on disk, because Parcelable does not guarantee the continuity of the data in the event of external change. Although serializable is inefficient, it is recommended to use serializable at this point.
Application Scenarios
It is necessary to pass some data between multiple parts (activity or service) through intent, and simple types (such as numbers, strings) can be placed directly into intent. Complex types must implement the Parcelable interface.
Serializable and the following questions need to be noted:
Public classTestImplementsSerializable {Private Static Final LongSerialversionuid = 1L; Public Static intStaticvar = 5; Public Static voidMain (string[] args) {Try { //Staticvar at initial time is 5ObjectOutputStream out =NewObjectOutputStream (NewFileOutputStream ("Result.obj")); Out.writeobject (NewTest ()); Out.close (); //modified to ten after serializationTest.staticvar = 10; ObjectInputStream oin=NewObjectInputStream (NewFileInputStream ("Result.obj")); Test T=(Test) oin.readobject (); Oin.close (); //re-read, print new values via T.staticvarSystem.out.println (T.staticvar); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } }}
The Main method in Listing 2, after serializing the object, modifies the value of the static variable, reads the serialized object, and then obtains the value of the static variable and prints it out through the object being read. According to Listing 2, does the SYSTEM.OUT.PRINTLN (t.staticvar) statement output 10 or 5?
The final output is 10, for the incomprehensible reader that the printed Staticvar is obtained from the Read object, it should be the state at the time of the save. The reason for printing 10 is that when serializing, the static variable is not saved, which is easier to understand, the serialization holds the state of the object, the static variable belongs to the state of the class, so the serialization does not save the static variable.
Case 2:
The function of the Ransient keyword is to control the serialization of variables, add the keyword before the variable declaration, you can prevent the variable from being serialized into the file, after being deserialized, the value of the transient variable is set to the initial value, such as the int type is 0, the object type is null.
Java Android serial number serializable and parcelable