android中传递复杂参数,activity之间和fragment之间的bundle传递集合/对象

来源:互联网 发布:蘑菇街算法工程师 编辑:程序博客网 时间:2024/04/29 02:54

在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方式:

[java] view plain copy
 
 
  1. public class StudentSer implements Serializable {  
  2.   
  3.     private String name;  
  4.     private int age;  
  5.     private String habby;  
  6.     private String title;  
  7.   
  8.     public StudentSer() {  
  9.         super();  
  10.     }  
  11.   
  12.     public StudentSer(String name, int age, String habby, String title) {  
  13.         super();  
  14.         this.name = name;  
  15.         this.age = age;  
  16.         this.habby = habby;  
  17.         this.title = title;  
  18.     }  
  19.   
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.   
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.   
  28.     public int getAge() {  
  29.         return age;  
  30.     }  
  31.   
  32.     public void setAge(int age) {  
  33.         this.age = age;  
  34.     }  
  35.   
  36.     public String getHabby() {  
  37.         return habby;  
  38.     }  
  39.   
  40.     public void setHabby(String habby) {  
  41.         this.habby = habby;  
  42.     }  
  43.   
  44.     public String getTitle() {  
  45.         return title;  
  46.     }  
  47.   
  48.     public void setTitle(String title) {  
  49.         this.title = title;  
  50.     }  
  51.   
  52.     @Override  
  53.     public String toString() {  
  54.         return "Student [name=" + name + ", age=" + age + ", habby=" + habby  
  55.                 + ", title=" + title + "]";  
  56.     }  
  57.   
  58. }  
第二步;传递序列化对象对象

[java] view plain copy
 
 
  1. // 点击跳转第二个Acitivity  
  2. Intent intent = new Intent(MainActivity.this,  
  3.         SecondActivity.class);  
  4. Bundle bundle = new Bundle();  
  5. bundle.putSerializable("data", data);  
  6. intent.putExtras(bundle);  
  7. startActivity(intent);  
第三步;接收序列化对象

[java] view plain copy
 
 
  1. // 获取bundle传递过来的数据  
  2.         Intent intent = getIntent();  
  3.         data = (ArrayList<StudentSer>) intent.getSerializableExtra("data");  


Activity传递参数到fragment

Parcelable方式
第一步 ;定义序列化实体类

[java] view plain copy
 
 
  1. public class StudentPar implements Parcelable {  
  2.   
  3.     private String name;  
  4.     private int age;  
  5.     private String habby;  
  6.     private String title;  
  7.   
  8.     public StudentPar() {  
  9.         super();  
  10.     }  
  11.   
  12.     public StudentPar(String name, int age, String habby, String title) {  
  13.         super();  
  14.         this.name = name;  
  15.         this.age = age;  
  16.         this.habby = habby;  
  17.         this.title = title;  
  18.     }  
  19.   
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.   
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.   
  28.     public int getAge() {  
  29.         return age;  
  30.     }  
  31.   
  32.     public void setAge(int age) {  
  33.         this.age = age;  
  34.     }  
  35.   
  36.     public String getHabby() {  
  37.         return habby;  
  38.     }  
  39.   
  40.     public void setHabby(String habby) {  
  41.         this.habby = habby;  
  42.     }  
  43.   
  44.     public String getTitle() {  
  45.         return title;  
  46.     }  
  47.   
  48.     public void setTitle(String title) {  
  49.         this.title = title;  
  50.     }  
  51.   
  52.     @Override  
  53.     public String toString() {  
  54.         return "Student [name=" + name + ", age=" + age + ", habby=" + habby  
  55.                 + ", title=" + title + "]";  
  56.     }  
  57.   
  58.     @Override  
  59.     public int describeContents() {  
  60.         // TODO Auto-generated method stub  
  61.         return 0;  
  62.     }  
  63.   
  64.     public static final Parcelable.Creator<StudentPar> CREATOR = new Creator<StudentPar>() {  
  65.   
  66.         @Override  
  67.         public StudentPar createFromParcel(Parcel source) {  
  68.             // TODO Auto-generated method stub  
  69.   
  70.             StudentPar stu = new StudentPar();  
  71.             stu.name = source.readString();  
  72.             stu.age = source.readInt();  
  73.             stu.habby = source.readString();  
  74.             stu.title = source.readString();  
  75.             return stu;  
  76.   
  77.         }  
  78.   
  79.         @Override  
  80.         public StudentPar[] newArray(int size) {  
  81.             // TODO Auto-generated method stub  
  82.             return new StudentPar[size];  
  83.         }  
  84.     };  
  85.   
  86.     /** 讲实体类数据写入Parcel */  
  87.     @Override  
  88.     public void writeToParcel(Parcel dest, int flags) {  
  89.         // TODO Auto-generated method stub  
  90.   
  91.         dest.writeString(name);  
  92.         dest.writeInt(age);  
  93.         dest.writeString(habby);  
  94.         dest.writeString(title);  
  95.     }  
  96.   
  97. }  
第二步,传递序列化对象

[java] view plain copy
 
 
  1. Fragment fragment = new MyFragment();  
  2.                 FragmentManager manager = getSupportFragmentManager();  
  3.                 FragmentTransaction ft = manager.beginTransaction();  
  4.                 Bundle bundle = new Bundle();  
  5.                 bundle.putParcelableArrayList("fragData", fragData);// ("fragData",fragData);  
  6.                 fragment.setArguments(bundle);  
  7.                 ft.replace(R.id.fram, fragment, "myFragment");  
  8.                 ft.commit();  

第三步;接收参数

[java] view plain copy
 
 
  1. Bundle bundle = getArguments();  
  2.         ArrayList<StudentPar> fragData = bundle.getParcelableArrayList("fragData");  


总结,传递对象可以通过Serializable和Parcelable,无论是Activity之间还是fragment之间或者Activity和fragment之间的参数传递,都大同小异,切记被传递的对象要实现Serializable和Parcelable;Serializable相对比较简单很多。使用哪一种方式视情况而定。

转自:http://blog.csdn.net/Kern_/article/details/45974527

0 0