Android中传递对象的三种方法

Android知识、前端、后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过!

Android中,Activity和Fragment之间传递对象,可以通过将对象序列化并存入Bundle或者Intent中进行传递,也可以将对象转化为JSON字符串,进行传递。

序列化对象可以使用Java的Serializable的接口、Parcelable接口。转化成JSON字符串,可以使用Gson等库。

1.Serializable

Model

  1. public class Author implements Serializable{
  2.     private int id;
  3.  
  4.     private String name;
  5.  
  6.     //...
  7. }
  1. public class Book implements Serializable{
  2.     private String title;
  3.     private Author author;
  4.     //...
  5. }

传递数据

 

  1.   Book book=new Book(); 
  2.   book.setTitle("Java编程思想"); 
  3.   Author author=new Author(); 
  4.   author.setId(1); 
  5.   author.setName("Bruce Eckel"); 
  6.   book.setAuthor(author); 
  7.   Intent intent=new Intent(this,SecondActivity.class); 
  8.   intent.putExtra("book",book); 
  9.   startActivity(intent);

接收数据

  1.  Book book= (Book) getIntent().getSerializableExtra("book");
  2.  Log.d(TAG,"book title->"+book.getTitle());
  3.  Log.d(TAG,"book author name->"+book.getAuthor().getName());

2.转化为JSON字符串

Model

  1. public class Author{
  2.     private int id;
  3.  
  4.     private String name;
  5.  
  6.     //...
  7. }
  8. public class Book{
  9.     private String title;
  10.     private Author author;
  11.     //...
  12. }

传递数据

  1. Book book=new Book();
  2. book.setTitle("Java编程思想");
  3. Author author=new Author();
  4. author.setId(1);
  5. author.setName("Bruce Eckel");
  6. book.setAuthor(author);
  7. Intent intent=new Intent(this,SecondActivity.class);
  8. intent.putExtra("book",new Gson().toJson(book));
  9. startActivity(intent);

接收数据

  1. String bookJson=getIntent().getStringExtra("book");
  2. Book book=new Gson().fromJson(bookJson,Book.class);
  3. Log.d(TAG,"book title->"+book.getTitle());
  4. Log.d(TAG,"book author name->"+book.getAuthor().getName());

3.使用Parcelable

实现Parcelable接口需要实现两个方法

  • describeContents方法。内容接口描述,默认返回0就可以;

  • writeToParcel方法。将传递的数据打包到Parcel容器中。

除了要实现这两个方法还必须创建一个Parcelable.Creator接口的实例,用于读取Parcel容器中的数据

Model

  1. public class Author implements Parcelable{
  2.     private int id;
  3.  
  4.     private String name;
  5.  
  6.     //setter & getter...
  7.  
  8.     @Override
  9.     public int describeContents() {
  10.  
  11.         return 0;
  12.     }
  13.  
  14.     @Override
  15.     public void writeToParcel(Parcel dest, int flags) {
  16.         //该方法将类的数据写入外部提供的Parcel中.即打包需要传递的数据到Parcel容器保存,
  17.         // 以便从parcel容器获取数据
  18.         dest.writeString(name);
  19.         dest.writeInt(id);
  20.  
  21.     }
  22.     public static final Creator<Author> CREATOR=new Creator<Author>() {
  23.         @Override
  24.         public Author createFromParcel(Parcel source) {
  25.             //从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层。
  26.             Author author=new Author();
  27.             author.setName(source.readString());
  28.             author.setId(source.readInt());
  29.             return author;
  30.         }
  31.  
  32.         @Override
  33.         public Author[] newArray(int size) {
  34.             //创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。方法是供外部类反序列化本类数组使用。
  35.             return new Author[size];
  36.         }
  37.     };
  38. }
  1. public class Book implements Parcelable{
  2.     private String title;
  3.     private Author author;
  4.     //setter & getter...
  5.  
  6.     @Override
  7.     public int describeContents() {
  8.         return 0;
  9.     }
  10.  
  11.     @Override
  12.     public void writeToParcel(Parcel dest, int flags) {
  13.         dest.writeString(title);
  14.         dest.writeParcelable(author,flags);
  15.     }
  16.     public static final Creator<Book> CREATOR=new Creator<Book>() {
  17.         @Override
  18.         public Book createFromParcel(Parcel source) {
  19.             Book book=new Book();
  20.             book.setTitle(source.readString());
  21.             book.setAuthor(source.<Author>readParcelable(Author.class.getClassLoader()));
  22.             return book;
  23.         }
  24.  
  25.         @Override
  26.         public Book[] newArray(int size) {
  27.             return new Book[0];
  28.         }
  29.     };
  30. }

传递数据

  1. Book book=new Book();
  2. book.setTitle("Java编程思想");
  3. Author author=new Author();
  4. author.setId(1);
  5. author.setName("Bruce Eckel");
  6. book.setAuthor(author);
  7. Intent intent=new Intent(this,SecondActivity.class);
  8. intent.putExtra("book",book);
  9. startActivity(intent);

接收数据

  1. Book book=getIntent().getParcelableExtra("book");
  2. Log.d(TAG,"book title->"+book.getTitle());
  3. Log.d(TAG,"book author name->"+book.getAuthor().getName());

4.性能分析

经过测试,我们得到下图的效果

可以看出,通过转换为字符串的速度是最慢的。Seralizable次之,Parcelable比Seralizable快10倍。所以从性能上考 虑,我们必定优先选择Parcelable。但是Parcelable有大量重复的模板代码,如何简化这些操作,将是下面主要讲解的内容。

5.简化Parcel操作

如果你使用android Studio 可以通过安装android-parcelable-intellij-plugin插件,或者自己配置模板进行操作。

5.1 parceler

除了上面的操作,还有大量的第三方库来简化Parcelable操作。当然使用这些库也许会降低Parcelable的性能。Parceler就是这样一个库。

Parceler使用非常简单,在定义Model时用@Parcel进行注解,在传递数据的时候使用Parcelswrap方法来包装成一个Parcelable对象。获取数据时用Parcelsunwrap方法来获取对象。

Model

  1. @Parcel
  2.  
  3. public class Author {
  4.  
  5.    int id;
  6.  
  7.     String name;
  8.  
  9.     //setter & getter...
  10. }
  1. @Parcel
  2. public class Book {
  3.     String title;
  4.     Author author;
  5.     //setter & getter
  6. }

传递对象

  1. Book book=new Book();
  2. book.setTitle("Java编程思想");
  3. Author author=new Author();
  4. author.setId(1);
  5. author.setName("Bruce Eckel");
  6. book.setAuthor(author);
  7. Intent intent=new Intent(this,SecondActivity.class);
  8. intent.putExtra("book", Parcels.wrap(book));
  9. startActivity(intent);

接收对象

  1. Book book= Parcels.unwrap(getIntent().getParcelableExtra("book"));
  2. Log.d(TAG,"book title->"+book.getTitle());
  3. Log.d(TAG,"book author name->"+book.getAuthor().getName());

除了Parceler之外,还有如auto-parcel,ParcelableCodeGenerator,ParcelableGenerator等第三方库,这里我将不进行讲解,有兴趣的朋友,可以自行研究。

原文地址:https://www.cnblogs.com/wanghuaijun/p/6212055.html