First, pass the general data type
For example: Integer floating-point string and so on.
Intent intent=new Intent (); Intent.setclass (Mainactivity.this, Otheractivity.class);//putextra method can pass values of various data types The first parameter is the key value the second parameter is the value Intent.putextra ("Data", "I am kaming"); StartActivity (intent);
The data type that can be returned by the Getextras method to get the key value is bundle type bundle Bundle=getintent (). Getextras (); String data=bundle.getstring ("data"); LOG.D ("otheractivity", data);
Second, the Passing object
The first method (Serializable):
Intent intent=new Intent () intent.setclass (Mainactivity.this, otheractivity.class); obj obj=new obj (); Obj.setname (" Kaming ");//serializable type PutExtra method Intent.putextra (" obj ", obj); startactivity (intent);
Bundle bundle=getintent (). Getextras (), obj data= (obj) bundle.get ("obj"),//GETLOG.D ("Otheractivity", Data.getname ()) ;
Note: To implements the serializable interface in obj
The second method (parcelable):
Intent intent=new Intent (); Intent.setclass (Mainactivity.this, otheractivity.class); Obj2 obj2=new Obj2 (); obj2.setage ("Obj2.setname"); ("kaming"); Intent.putextra ("obj", obj2); startactivity (intent);
Package com.example.activity01;import android.os.parcel;import android.os.parcelable;public class Obj2 implements Parcelable{private String name;private String Age;public string getname () {return name;} Public void setname (String name) {this.name = name;} Public string getage () {return age;} Public void setage (String age) {this.age = age;} @Overridepublic int describecontents () {// TODO Auto-generated method stubreturn 0;} Public static parcelable.creator<obj2> creator=new creator<obj2> () {@ Overridepublic obj2 createfromparcel (Parcel source) {// todo auto-generated method stubobj2 obj2=new obj2 (); Obj2.setage (source.readstring ()); Obj2.setName ( Source.readstring ());//The Order of reading is as return obj2 as the order written below;} @Overridepublic obj2[] newarray (Int size) {// TODO Auto-generated method stubreturn null;}}; @Overridepublic void writetoparcel (parcel dest, int flags) {// TODO Auto-generated method stubdest.writestring (age);d est.writestring (name);}}
In Obj2 to implements Parcelable, complete two abstract methods;
Data can be written in the Writetoparcel method
Declare public static parcelable.creator<obj2> creator=new creator<obj2> () {}; generic class
Data can be read in the Createformparcel method
The difference between parcelable and serializable:
The role of Serializable is in order to save the object's properties to the local file, database, network stream, RMI for data transfer, of course, this transmission can be within the program or can be between two programs . The Android Parcelable is designed to be serializable inefficient, designed to transmit data efficiently between different components within the program and between different Android programs (AIDL) . These data exist only in memory, Parcelable is the carrier of messages that are communicated through IBinder.
Efficiency:
Parcelable has a better performance than serializable and is smaller in terms of memory overhead, sorecommended use of parcelable for data transfer between memory, such as the transfer of data between activity, and serializable can persist the data for easy preservation, soSelect Serializable when you need to save or transfer data over the network, because Android versions parcelable may be different, it is not recommended to use parcelable for data persistence.
PS: There is no parcelable this class in Java, only in Android.
Android Learning notes (v)