開發過程中,Activity之間傳遞數據是必不可少的,android中使用Intent和Bundle作為數據載體,在Activity之間傳遞,對於基礎數據類型,Bundle已經提供相關的put,get方法,而作為自定義的類型則需要有特別的要求.
自定義類型,想要使用Bundle傳遞時,需要滿足特定條件。即該類型需要實現Serializable接口或者Parcelable接口
(注意:如果目標對象中包含其他對象,則被包含的對象也需要實現Serializable接口或者Parcelable接口)
(關於Serializable接口和Parcelable接口就不在此做記錄了)
步驟:
1.定義類,實現相關接口
2.使用Intent和Bundle對應的方法set數據
3.startActivity傳遞Intent對象
使用Serializable
一.傳遞對象
a).定義類,實現Serializable接口

public class Student implements Serializable { // members private String name; private String age; private int id; // getter setter public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } /** * Serializable */ private static final long serialVersionUID = 1L; }
b).使用Intent傳遞對象

// 使用Serializable Button btnSerializable = (Button)findViewById(R.id.btnSerializable); btnSerializable.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // create data Student student = new Student(); student.setName("stephen lee"); student.setAge("12"); student.setId(1); // bundle Bundle bundle = new Bundle(); bundle.putSerializable(MainActivity.StudentKey,student); // intent Intent intent = new Intent(arg0.getContext(),SecondActivity.class); intent.putExtras(bundle); // navigate startActivity(intent); } });
c).獲取傳遞的數據

Bundle bundle = this.getIntent().getExtras(); Student student = (Student)bundle.getSerializable(MainActivity.StudentKey); if(student!=null) textView.setText("name:" + student.getName() + "age:" + student.getAge() + "id:" + student.getId());
二.傳遞對象集合
a).同上
b).同上(注意使用putSerializable()方法時,需要把List<>強轉成為Serializable,並且集合中的成員都需要實現Serializable接口)

Button btnSerializableList = (Button)findViewById(R.id.btnSerializableList); btnSerializableList.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // create data List<Student> students = new ArrayList<Student>(); Student student1 = new Student(); student1.setName("hellenism"); student1.setAge("12"); student1.setId(1); Student student2 = new Student(); student2.setName("stephen lee"); student2.setAge("12"); student2.setId(2); students.add(student1); students.add(student2); // bundle , 注意:使用putSerializable()方法時,需要把List<>強轉成為Serializable,並且集合中的成員都需要實現Serializable接口 Bundle bundle = new Bundle(); bundle.putSerializable(MainActivity.StudentsKey,(Serializable)students); // intent Intent intent = new Intent(v.getContext(),SecondActivity.class); intent.putExtras(bundle); // navigate startActivity(intent); } });
c).同上(不足之處在於,由於獲取數據時候,需要把Serializable強轉成為對應的List<>,此處會有警告)

Bundle bundle = this.getIntent().getExtras(); List<Student> students = (List<Student>)bundle.getSerializable(MainActivity.StudentsKey); if(students!=null) textView.setText("name:" + students.get(1).getName() + "age:" + students.get(1).getAge() + "id:" + students.get(1).getId());
使用Parcelable
一.傳遞對象
a).定義類,實現Parcelable接口

public class Person implements Parcelable { // members private String name; private String age; private int id; // getter setter public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } // Parcelable @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.writeString(age); dest.writeInt(id); } public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() { public Person createFromParcel(Parcel source) { Person person = new Person(); person.age = source.readString(); person.name = source.readString(); person.id = source.readInt(); return person; } public Person[] newArray(int size) { return new Person[size]; } }; }
b).使用Intent傳遞參數

// 使用Parcelable Button btnParcelable = (Button)findViewById(R.id.btnParcelable); btnParcelable.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // create data Person person = new Person(); person.setName("i am person"); person.setAge("12"); person.setId(1); // bundle Bundle bundle = new Bundle(); bundle.putParcelable(MainActivity.PersonKey,person); // intent Intent intent = new Intent(arg0.getContext(),SecondActivity.class); intent.putExtras(bundle); // navigate startActivity(intent); } });
c).獲取傳遞的參數

Bundle bundle = this.getIntent().getExtras(); Person person = (Person)bundle.getParcelable(MainActivity.PersonKey); if(person!=null) textView.setText("name:" + person.getName() + "age:" + person.getAge() + "id:" + person.getId());
二.傳遞數據類型
a).同上
b).同上

Button btnParcelableList = (Button)findViewById(R.id.btnParcelableList); btnParcelableList.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // create data List<Person> persons = new ArrayList<Person>(); Person person1 = new Person(); person1.setAge("12"); person1.setName("stephen lee"); person1.setId(1); Person person2 = new Person(); person2.setAge("12"); person2.setName("hellenism"); person2.setId(2); persons.add(person1); persons.add(person2); // bundle Bundle bundle = new Bundle(); bundle.putParcelableArrayList(MainActivity.PersonsKey, (ArrayList<? extends Parcelable>) persons); // intent Intent intent = new Intent(v.getContext(),SecondActivity.class); intent.putExtras(bundle); // navigate startActivity(intent); } });
c).同上

Bundle bundle = this.getIntent().getExtras(); List<Person> persons = bundle.getParcelableArrayList(MainActivity.PersonsKey); if(persons!=null) textView.setText("name:" + persons.get(1).getName() + "age:" + persons.get(1).getAge() + "id:" + persons.get(1).getId());
注意:
使用以上方法傳遞對象時,都是創建了新的實例,而非傳遞對象的引用,如果是傳遞引用,也就無需序列化了。
完整例子: