Two Methods for Intent to pass objects, intent to pass objects
Android provides two methods for passing object parameter types for intent.
The entity classes must implement the Serializable and Parcelable interfaces respectively.
First, we need to know that to pass an object, we need to serialize the object first.
I. Why object serialization?
1. Permanently Save the object and save the object's byte sequence to a local file;
2. Use a serialized object to transmit objects in the network and between processes;
Ii. When to implement the Serializable interface and Parcelable interface of a serialized object
1. Parcelable cannot store data on disks, because Parcelable cannot store data continuously due to external changes.
Therefore, Serializable is recommended in this case.
2. When using the memory, Parcelable has higher performance than Serializable, so we recommend using the Parcelable class.
In addition, Serializable will generate a large number of temporary variables during serialization, resulting in frequent GC.
Bytes ----------------------------------------------------------------------------------------------------------
The following describes how to use the two object passing methods.
I. How the object class inherits the Serializable Interface
1. The first step is to implement the Serializable interface for the object class of the object to be transmitted.
1 package com. xqx. intentDemo; 2 3 import java. io. serializable; 4 5/** 6 * People entity class, contains three attributes: name, sex, and age, and implement class encapsulation 7 */8 public class People implements Serializable {9 private String name; 10 private String sex; 11 private int age; 12 13 public String getName () {14 return name; 15} 16 17 public String getSex () {18 return sex; 19} 20 21 public int getAge () {22 return age; 23} 24 25 public void setName (String name) {26 this. name = name; 27} 28 29 public void setSex (String sex) {30 this. sex = sex; 31} 32 33 public void setAge (int age) {34 this. age = age; 35} 36}
2. Data Transmission steps
// Create Intent object Intent intent = new Intent (); intent. setClass (MainActivity. this, NewActivity. class); // create object class People people = new People (); people. setName ("Mark"); people. setSex ("boy"); people. setAge (20); // Add the intent for transmitting data. putExtra ("people", people); startActivity (intent );
3. receive data
Intent intent = getIntent(); People people = (People) intent.getSerializableExtra("people"); Log.i("DATA_SHOW","name-->"+people.getName()+",sex-->"+people.getSex()+",age-->"+people.getAge());
4. Log Printing
15794-15794/? I/DATA_SHOW﹕ name-->Mark,sex-->boy,age-->20
Ii. How the object class inherits the Parcelable Interface
1. Implement the Parcelable interface of the object class
public class Student implements Parcelable {}
Two Methods of rewriting an Interface
@ Override public int describeContents () {return 0 ;}
// Set the attributes to be passed to Parcel parcel. writXxx format. For details, see the attribute type @ Override public void writeToParcel (Parcel parcel, int I) {parcel. writeString (name); parcel. writeString (sex); parcel. writeInt (age );}
Add a constant CREATOR (the name size must be fixed). This constant must implement the Parcelable Internal interface: Parcelable. Creator and implement the two methods in this interface.
public static final Parcelable.Creator<Student> CREATOR = new Creator<Student>() { @Override public Student createFromParcel(Parcel source) { Student student = new Student(); student.name = source.readString(); student.sex = source.readString(); student.age = source.readInt(); return student; } @Override public Student[] newArray(int size) { return new Student[size]; } };
2. Data Transmission
Intent intent = new Intent(); intent.setClass(MainActivity.this, NewActivity.class); Student student = new Student(); student.setName("Alice"); student.setSex("girl"); student.setAge(19); intent.putExtra("student",student); startActivity(intent);
3. receive data
Intent intent = getIntent(); Student student = intent.getParcelableExtra("student"); Log.i("DATA_SHOW","name-->"+student.getName()+",sex-->"+student.getSex()+",age-->"+student.getAge());
4. Log
1280-1280/? I/DATA_SHOW﹕ name-->Alice,sex-->girl,age-->19