1.对要传递的对象类型实现Parcelable:
public class User implements Parcelable//对象必须实现Parcelable接口才可以putExtra(或者序列化){ String name; String psw; public User(String name, String psw) { super(); this.name = name; this.psw = psw; } @Override public String toString() { return "User [name=" + name + ", psw=" + psw + "]"; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags)//将属性写入Parcel对象 { dest.writeString(name); dest.writeString(psw); } public static final Parcelable.CreatorCREATOR = new Parcelable.Creator () { public User createFromParcel(Parcel in) { return new User(in);//创建一个有参构造函数 } public User[] newArray(int size) { return new User[size]; } }; public User(Parcel in)//根据写入的顺序依次读取 { name = in.readString(); psw = in.readString(); }}
2.第一个activity传递user对象:
public class MainActivity extends Activity{ private EditText editText1; private EditText editText2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText1 = (EditText) findViewById(R.id.editText1); editText2 = (EditText) findViewById(R.id.editText2); findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String namestr = editText1.getText().toString(); String pswstr = editText2.getText().toString(); User user = new User(namestr, pswstr); Intent intent = new Intent(MainActivity.this, NextActivity.class); intent.putExtra("user", user);//传入对象,对象类必须实现Parcelable或者序列化 startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }}
3.第二个activity接收user对象:
public class NextActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_next); TextView textView = (TextView) findViewById(R.id.textView1); Intent intent = getIntent(); User user = intent.getParcelableExtra("user");//得到一个对象 textView.setText(user.toString()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_next, menu); return true; }}
其他笔记:
1.带有数组类型属性的类
@Override public void writeToParcel(Parcel dest, int flags) { dest.writeStringList(CompanyIntroList); } public EstateInfoJson(Parcel in) { CompanyIntroList = new ArrayList(); in.readStringList(CompanyIntroList); }
2.带有其他类的属性
public EstateParamJson EstateParam; @Override public void writeToParcel(Parcel dest, int flags) { dest.writeValue(EstateParam); } @Override public EstateInfoJson(Parcel in) { EstateParam = (EstateParamJson) in.readValue(EstateParamJson.class.getClassLoader()); }
3.带有其他类的数组类型的属性
public ArrayListshiJing;//实景图 @Override public void writeToParcel(Parcel dest, int flags) { dest.writeTypedList(shiJing); } public GalleryJson(Parcel in) { shiJing = new ArrayList (); in.readTypedList(shiJing, PicJson.CREATOR); }
4.带有子类父类的情况:
public abstract class A implements Parcelable { private int a; protected A(int a) { this.a = a; } public void writeToParcel(Parcel out, int flags) { out.writeInt(a); } protected A(Parcel in) { a = in.readInt(); } } public class B extends A { private int b; public B(int a, int b) { super(a); this.b = b; } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public B createFromParcel(Parcel in) { return new B(in); } public B[] newArray(int size) { return new B[size]; } }; public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeInt(b); } private B(Parcel in) { super(in); b = in.readInt(); } }
参考:http://dev.wo.com.cn/docportal/doc_queryMdocDetail.action?mdoc.docindex=6301