An example of this article is the method of using intent to pass objects in Android programming. Share to everyone for your reference, specific as follows:
In the previous article, we introduced the use of intent, such as starting activities, sending broadcasts, inspiring services, and passing some data when using intent. As shown in the following code:
Intent Intent = new Intent (this,secondactivity.class);
Intent.putextra ("info", "I am Fine");
StartActivity (Intent);
When passing data, the method used is Putextra, the supported data type is limited, how to pass the object??
In Android, there are two ways to pass an object using intent: Serializable serialization and the way Parcelable is serialized.
1, Serializable Way
This means that an object is converted to a stored or removable state, and the serialized object can be transferred over the network and stored locally.
Object serialization, you only need to implement the Serializable class.
Package com.example.testapplication;
Import java.io.Serializable;
/** *
Object Serialization
* @author yy * * */Public
class Emp implements Serializable {
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;
}
}
So intent how to pass object parameters, see the API found the following methods:
Copy Code code as follows:
Intent.putextra (String name, Serializable value);
Therefore, this method is used to pass as follows:
Intent Intent = new Intent (this,secondactivity.class);
Intent.putextra ("obj", new Emp ());
StartActivity (Intent);
So how do we get it? Use the following methods:
Copy Code code as follows:
EMP emp = (EMP) getintent (). Getserializableextra ("obj");
This will get the EMP object.
2, Parcelable Way
The principle of this method is to decompose a complete object so that each part of the decomposition is the data type supported by intent. Examples are as follows:
Package com.example.testapplication;
Import Android.os.Parcel;
Import android.os.Parcelable;
/** * parcelable mode * @author yy */public class EMP2 implements parcelable{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;
@Override public int describecontents () {return 0;
@Override public void Writetoparcel (Parcel dest, int flag) {//write out name dest.writestring (name);
Write The Age dest.writeint (age); public static final parcelable.creator<emp2> Creator = new creator<emp2> () {@Override public Emp2
[] NewArray (int size) {return new emp2[size];
@Override public Emp2 Createfromparcel (Parcel source) {EMP2 emp2 = new EMP2 ();
The order of the reads is in accordance with the order written above and read the name emp2.name = Source.readstring (); EmP2.age = Source.readint ();
return EMP2;
}
};
}
Passing objects: The same way as serialization:
Intent Intent = new Intent (this,secondactivity.class);
Intent.putextra ("obj", new EMP2 ());
StartActivity (Intent);
Get object:
Copy Code code as follows:
EMP2 emp2 = Getintent (). Getparcelableextra ("obj");
3, the difference
Serializable produces a large number of temporary variables when serialized, resulting in frequent GC. Therefore, when using memory, parcelable analogy serializable performance is high, so it is recommended to use Parcelable class.
Parcelable cannot be used in situations where data is stored on disk because Parcelable does not guarantee the continuity of the data in the event of a change in the environment. Although serializable efficiency is low, also do not advocate use, but in this case, still suggest you use serializable.
I hope this article will help you with the Android program.