Original URL: http://blog.csdn.net/lixiang0522/article/details/8642202
The objects passed between components in Android generally implement the Parcelable interface, of course, you can also use the Java serializable interface, the former is specifically designed for Android, more efficient, below we will implement a parcelabel. 1. Create a class to implement the Parcelable interface, which is implemented as follows:
[Java]View PlainCopy
- Package com.hebaijun.testparcelable;
- Import Android.os.Parcel;
- Import android.os.Parcelable;
- Public class Parcelabledata implements parcelable{
- private String name;
- private int age;
- Public Parcelabledata () {
- Name = "Guest";
- Age = 20;
- }
- Public Parcelabledata (Parcel in) {
- //order in the same order as the Writetoparcel write
- Name = In.readstring ();
- Age = In.readint ();
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- public int getage () {
- return age;
- }
- public void Setage (int.) {
- this.age = age;
- }
- @Override
- public int describecontents () {
- //TODO auto-generated method stub
- return 0;
- }
- @Override
- public void Writetoparcel (Parcel dest, int flags) {
- //TODO auto-generated method stub
- Dest.writestring (name);
- Dest.writeint (age);
- }
- public static final parcelable.creator<parcelabledata> Creator = new parcelable.creator< Parcelabledata> () {
- Public Parcelabledata Createfromparcel (Parcel in) {
- return new Parcelabledata (in);
- }
- Public parcelabledata[] NewArray (int size) {
- return new parcelabledata[size];
- }
- };
- }
2. Send the object by the following method. The bundle class also implements the Parcelable interface, typically in Android we encapsulate the data and transmit it through bundles.
[Java]View PlainCopy
- Intent Intent = new Intent ();
- Intent.setclass (This, subactivity. Class);
- Add directly
- Intent.putextra ("MyData", New Parcelabledata ());
- Through bundles
- Bundle bundle = new bundle ();
- Bundle.putstring ("MyString", "Test bundle");
- Bundle.putparcelable ("MyData", new Parcelabledata ());
- Intent.putextras (bundle);
- StartActivity (Intent);
3. The following method of receiving the object.
[Java]View PlainCopy
- Parcelabledata parcelabledata = Getintent (). Getparcelableextra ("MyData");
- Bundle bundle = Getintent (). Getextras ();
- Parcelabledata parcelabledata = bundle.getparcelable ("MyData");
- String testbundlestring = bundle.getstring ("MyString");
- LOG.V ("string=", testbundlestring);
- LOG.V ("Name=", Parcelabledata.getname ());
- LOG.V ("age=", " " +parcelabledata.getage ());
The transferred object needs to be serialized: There are two ways to implement the Serializable interface, which is the original Java mode, and the other is the parcelable mode of Android, this performance may be better, I guess, But this is required to manually write the implementation of the Parcelable interface.
Serializable Storage data:
- Person Mperson = new Person ();
- Mperson.setname ("Frankie");
- Mperson.setage (25);
- Intent mintent = new Intent (this,objecttrandemo1. Class);
- Bundle Mbundle = new Bundle ();
- Mbundle.putserializable (Ser_key,mperson);
- Mintent.putextras (Mbundle);
Serializable fetch data:
// 获取启动该ResultActivity的Intent |
24 |
Intent intent = getIntent(); |
26 |
Bundle bundle = intent.getExtras(); |
28 |
Person person = (Person) bundle.getSerializable( "person" ); |
Parcelable Storage Data:
- Intent mintent = new Intent (This,objecttrandemo2. Class);
- Bundle Mbundle = new Bundle ();
- Mbundle.putparcelable (Par_key, Mbook);
- Mintent.putextras (Mbundle);
parcelable Fetch data:
Book Mbook = (book) getintent (). Getparcelableextra (Objecttrandemo.par_key);
Reference 1:http://blog.csdn.net/android_tutor/article/details/5740845
Reference 2:http://my.oschina.net/u/577632/blog/76906
"Go" the use of intent transitive objects and bundles in Android