Intent 可以携带对象来在不同的Activity中传递数据,要传递的数据必须实现三个方法
1. describeContents()返回0就可以
2. writeToParcel(Parcel parcel, int flag) 把需要写入的数据写入Parcel中,然后框架调用此方法发送给接收端的Activity
3. public static final Parcelable.Creator<ParcelableObject> CREATOR 外部类反序列化该类时调用此方法
注意:写入parcel和反序列化parcel时顺序一定要相同,不然数据会出错。
1. /**
2. * ParcelableObject.java
3. * com.androidtest.parcelable
4. *
5. * Function: TODO
6. *
7. * ver date author
8. * ──────────────────────────────────
9. * 2011-5-11 Leon
10. *
11. * Copyright (c) 2011, TNT All Rights Reserved.
12. */
13.
14. package com.androidtest.parcelable;
15.
16. import android.os.Parcel;
17. import android.os.Parcelable;
18.
19. /**
20. * ClassName:ParcelableObject 需要实现三个方法,describeContents()返回0就可以 、 writeToParcel(Parcel parcel, int flag)
21. * 写入Parcel和public static final Parcelable.Creator
22. <parcelableobject><span style="font-size: 14px"> CREATOR 供外部类反序列化该类
23. * 一定要注意顺序后面这个两个函数读和写的顺序要相同,不然数据会出错
24. * Function: TODO
25. * Reason: TODO ADD REASON
26. *
27. * @author Leon
28. * @version
29. * @since Ver 1.1
30. * @Date 2011-5-11
31. */
32. public class ParcelableObject implements Parcelable{
33. private String name ;
34. private String surName ;
35. private int age ;
36.
37. public String getName() {
38. return name;
39. }
40.
41. public void setName(String name) {
42. this.name = name;
43. }
44.
45. public String getSurName() {
46. return surName;
47. }
48.
49. public void setSurName(String surName) {
50. this.surName = surName;
51. }
52.
53. public int getAge() {
54. return age;
55. }
56.
57. public void setAge(int age) {
58. this.age = age;
59. }
60.
61. @Override
62. public int describeContents() {
63.
64. // TODO Auto-generated method stub
65. return 0;
66.
67. }
68.
69. @Override
70. public void writeToParcel(Parcel dest, int flag) {
71.
72. // TODO Auto-generated method stub
73.
74. dest.writeInt(age);
75. dest.writeString(name);
76. dest.writeString(surName);
77.
78. }
79. public static final Parcelable.Creator CREATOR = new Creator() {
80.
81. @Override
82. public ParcelableObject createFromParcel(Parcel source) {
83.
84. // TODO Auto-generated method stub
85. ParcelableObject parcelableObj = new ParcelableObject();
86. parcelableObj.age=source.readInt();
87. parcelableObj.surName=source.readString();
88. parcelableObj.name = source.readString();
89. return parcelableObj;
90.
91. }
92.
93. @Override
94. public ParcelableObject[] newArray(int size) {
95.
96. // TODO Auto-generated method stub
97. return new ParcelableObject[size];
98.
99. }
100.
101. };
102. }
ParcelableActivity1 发送的代码
1. package com.androidtest.parcelable;
2.
3. import android.app.Activity;
4. import android.content.Intent;
5. import android.os.Bundle;
6. import android.view.View;
7. import android.view.View.OnClickListener;
8. import android.widget.Button;
9. import android.widget.LinearLayout;
10. public class ParcelableActivity1 extends Activity {
11.
12. @Override
13. protected void onCreate(Bundle savedInstanceState) {
14. // TODO Auto-generated method stub
15. super.onCreate(savedInstanceState);
16. LinearLayout linearLayout = new LinearLayout(this);
17. Button button = new Button(this);
18. button.setText("Send Parcelable Object");
19. linearLayout.addView(button);
20. button.setOnClickListener(new OnClickListener(){
21. @Override
22. public void onClick(View arg0) {
23. ParcelableObject passObject = new ParcelableObject();
24. passObject.setName("Leon Zhang");
25. passObject.setAge(18);
26. passObject.setSurName("xxx");
27.
28. Intent intent = new Intent(ParcelableActivity1.this,ParcelableActivity2.class);
29. // 使用Bundle发送
30. // Bundle bundle = new Bundle();
31. // bundle.putParcelable("key", passObject);
32. intent.putExtra("key", passObject);
33. // intent.putExtras(bundle);
34. intent.putExtra("key2", "out of bundle");
35. startActivity(intent);
36. }
37. });
38. this.setContentView(linearLayout);
39. }
40.
41. }
ParcelableActivity2 接收的代码
1. package com.androidtest.parcelable;
2.
3. import android.app.Activity;
4. import android.os.Bundle;
5. import android.util.Log;
6.
7. public class ParcelableActivity2 extends Activity {
8.
9. private static final String TAG = ParcelableActivity2.class.getSimpleName();
10.
11. @Override
12. protected void onCreate(Bundle savedInstanceState) {
13.
14. // TODO Auto-generated method stub
15. super.onCreate(savedInstanceState);
16. ParcelableObject passObject = this.getIntent()
17. .getParcelableExtra("key");
18.
19. Log.v(TAG, "Name: " + passObject.getName() + " Surname: "
20. + passObject.getSurName() + " Age: "
21. + String.valueOf(passObject.getAge())
22. + " out of bundle is :"
23. + this.getIntent().getStringExtra("key2"));
24. Bundle myBundle = this.getIntent().getExtras();
25. Log.v(TAG,"XX: "
26. + ((ParcelableObject) myBundle.getParcelable("key"))
27. .getName()+ " Key2 :"+ myBundle.getString("key2"));
28.
29. }
30.
31. }
当使用Intent传递数据时,如果你自己创建了一个Bundle,则无论要传的数据是否放入Bundle(如直接intent.putExtra("xx" , "xx")),都会放到放到此Bundle中。如果没有创建此Bundle,则会默认创建一个Bundle来存放数据。当取数据时可以先取Bundle再从Bundle中取数据,或者直接取就好了。
如果要发送对象最简单的方法是:把要发的数据直接当做对象发送,接收也不用Bundle,因为已经默认创建