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等第三方庫,這里我將不進行講解,有興趣的朋友,可以自行研究。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM