在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 + "]";
- }
-
- }
第二步;传递序列化对象对象
-
- Intent intent = new Intent(MainActivity.this,
- SecondActivity.class);
- Bundle bundle = new Bundle();
- bundle.putSerializable("data", data);
- intent.putExtras(bundle);
- startActivity(intent);
第三步;接收序列化对象
-
- 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() {
-
- return 0;
- }
-
- public static final Parcelable.Creator<StudentPar> CREATOR = new Creator<StudentPar>() {
-
- @Override
- public StudentPar createFromParcel(Parcel source) {
-
-
- 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) {
-
- return new StudentPar[size];
- }
- };
-
-
- @Override
- public void writeToParcel(Parcel dest, int flags) {
-
-
- 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);
- fragment.setArguments(bundle);
- ft.replace(R.id.fram, fragment, "myFragment");
- ft.commit();
第三步;接收参数
- Bundle bundle = getArguments();
- ArrayList<StudentPar> fragData = bundle.getParcelableArrayList("fragData");
总结,传递对象可以通过Serializable和Parcelable,无论是Activity之间还是fragment之间或者Activity和fragment之间的参数传递,都大同小异,切记被传递的对象要实现Serializable和Parcelable;Serializable相对比较简单很多。使用哪一种方式视情况而定。
转自:http://blog.csdn.net/Kern_/article/details/45974527