在Android开发中,Activity之间通过Intent使用bundle,fragment之间和Activityty通过setArguments使用bundle,对于一些简单的参数传递比较简单,而且方式也有多种,这个就不介绍了。在这里介绍一下复杂的参数传递,比如传递集合ArrayList,对象ArrayList<Object>。
无论是Activity之间参数传递,还是Fragment之间参数传递,或者是Activity与Fragment之间,都要使用Bundle,方式基本相同。Bundle在这里就相当与一个介质,把数据捆绑在一起;
但是对于一些对象的传递,我们则需要把被传递的对象需要先实现序列化,而序列化对象有两种方式:java.io.Serializable和android.os.Parcelable
Java中使用的是Serializable,而谷歌在Android使用了自定义的Parcelable。
两种序列化方式的区别:
1.在使用内存的时候,Parcelable比Serializable性能高,推荐使用Parcelable类;
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC;
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下,这种情况建议使用Serializable。
Activity跳转到另一个Activity,
第一步 ;定义序列化实体类
Serializable方式:
public class StudentSer implements Serializable { private String name; private int age; private String habby; private String title; public StudentSer() { super(); } public StudentSer(String name, int age, String habby, String title) { super(); this.name = name; this.age = age; this.habby = habby; this.title = title; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getHabby() { return habby; } public void setHabby(String habby) { this.habby = habby; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", habby=" + habby + ", title=" + title + "]"; }}第二步;传递序列化对象对象
// 点击跳转第二个Acitivity Intent intent = new Intent(MainActivity.this, SecondActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable("data", data); intent.putExtras(bundle); startActivity(intent);第三步;接收序列化对象
// 获取bundle传递过来的数据 Intent intent = getIntent(); data = (ArrayList<StudentSer>) intent.getSerializableExtra("data");
Activity传递参数到fragment
Parcelable方式
第一步 ;定义序列化实体类
public class StudentPar implements Parcelable { private String name; private int age; private String habby; private String title; public StudentPar() { super(); } public StudentPar(String name, int age, String habby, String title) { super(); this.name = name; this.age = age; this.habby = habby; this.title = title; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getHabby() { return habby; } public void setHabby(String habby) { this.habby = habby; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", habby=" + habby + ", title=" + title + "]"; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } public static final Parcelable.Creator<StudentPar> CREATOR = new Creator<StudentPar>() { @Override public StudentPar createFromParcel(Parcel source) { // TODO Auto-generated method stub StudentPar stu = new StudentPar(); stu.name = source.readString(); stu.age = source.readInt(); stu.habby = source.readString(); stu.title = source.readString(); return stu; } @Override public StudentPar[] newArray(int size) { // TODO Auto-generated method stub return new StudentPar[size]; } }; /** 讲实体类数据写入Parcel */ @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeString(name); dest.writeInt(age); dest.writeString(habby); dest.writeString(title); }}第二步,传递序列化对象
Fragment fragment = new MyFragment(); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction ft = manager.beginTransaction(); Bundle bundle = new Bundle(); bundle.putParcelableArrayList("fragData", fragData);// ("fragData",fragData); fragment.setArguments(bundle); ft.replace(R.id.fram, fragment, "myFragment"); ft.commit();
第三步;接收参数
Bundle bundle = getArguments(); ArrayList<StudentPar> fragData = bundle.getParcelableArrayList("fragData");
好了,欢迎大家交流指正。附下Demo,如下:
点击打开链接http://download.csdn.net/detail/kern_/8738587
转载请出示出处;