Serializable and Parcelable implement serialization,
Serializable
Serializable is a serialization interface provided by Java. During implementation, you should specify the serialVersionUID of the object.
How to quickly enter UID in Android Studio:
File-> Settings-> Editor-> Inspections-> Java-> Serialization
Issues-> Serializable class without 'serialversionuid' check this option to Enter the class that implements Serializable, select the class name, Alt + Enter pop-up prompt, and then import directly
Implement serialization and deserialization:
public class Student implements Serializable{ private static final long serialVersionUID = 851965803178397333L; public Student(String name, int age) { this.name = name; this.age = age; } public Student(String adress, String name, int age) { this.adress = adress; this.name = name; this.age = age; } private String adress; private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
Public class myClass {private static Student newStudent; public static void main (String [] args) {Student student = new Student ("", 22 ); try {ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ("cache"); out. writeObject (student); out. close ();} catch (Exception e) {e. printStackTrace ();} try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace ();} try {ObjectInputStream out = new ObjectInputStream (new FileInputStream ("cache"); newStudent = (Student) out. readObject (); out. close ();} catch (Exception e) {e. printStackTrace ();} System. out. println (newStudent. equals (student ));}}
Output: false
The restored newStudent and student have the same content, but they are not the same object.
If serialVersionUID is not specified, an exception is reported when the object structure changes.
public class Student implements Serializable{ public Student(String adress, String name, int age) { this.adress = adress; this.name = name; this.age = age; } private String adress; private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
Remove the serialVersionUID field and add a new constructor.
An exception is reported during deserialization:
java.io.InvalidClassException: com.example.lib.Student; local class incompatible: stream classdesc serialVersionUID = 851965803178397333, local class serialVersionUID = -67645744269838304
Note:
Static member variables belong to Classes and do not belong to objects, so they are not involved in the serialization process.
Member variables marked with the transient keyword are not involved in the serialization process.
Parcelable
This is an interface in Android, which can be passed through Intent and Binder.
public class Student implements Parcelable{ @Override public String toString() { return "Student{" + "adress=" + adress + ", name='" + name + '\'' + ", age=" + age + '}'; } private Adress adress; private String name; private int age; public Student(Adress adress, String name, int age) { this.adress = adress; this.name = name; this.age = age; } protected Student(Parcel in) { System.out.println("Student.Student"); adress = in.readParcelable(Adress.class.getClassLoader()); name = in.readString(); age = in.readInt(); } @Override public void writeToParcel(Parcel dest, int flags) { System.out.println("Student.writeToParcel"); dest.writeParcelable(adress, flags); dest.writeString(name); dest.writeInt(age); } @Override public int describeContents() { System.out.println("Student.describeContents"); return 0; } public static final Creator
CREATOR = new Creator
() { @Override public Student createFromParcel(Parcel in) { System.out.println("Student.createFromParcel"); return new Student(in); } @Override public Student[] newArray(int size) { System.out.println("Student.newArray"); return new Student[size]; } };}
public class Adress implements Parcelable { private String adress; public Adress(String adress) { this.adress = adress; } protected Adress(Parcel in) { adress = in.readString(); } public static final Creator CREATOR = new Creator() { @Override public Adress createFromParcel(Parcel in) { return new Adress(in); } @Override public Adress[] newArray(int size) { return new Adress[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(adress); }}
Public void Jump (View view) {Student student = new Student (new Adress ("China"), "yaoyan", 22); Intent intent = new Intent (this, SecondActiivity. class); Bundle bundle = new Bundle (); bundle. putParcelable ("yaoyan", student); System. out. println ("MainActivity. jump "); intent. putExtra ("mainActivity", bundle); startActivity (intent );}
public class SecondActiivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent intent = getIntent(); Bundle bundle = intent.getBundleExtra("mainActivity"); System.out.println("SecondActiivity.onCreate"); Student yaoyan = bundle.getParcelable("yaoyan"); System.out.println("yaoyan = " + yaoyan); }}
From the print order, the writeToParcel method is called during Object serialization. In the Activity (SecondActiivity) after the jump, the deserialization method createFromParcel is called when the object is obtained.
As mentioned in the exploration of Android development Art:
Serializable: it is easy to use but costly. serialization and deserialization require a large number of I/O operations.
Parcelable: high efficiency. It is a serialization method recommended by Android, but it is difficult to operate.