广告
返回顶部
首页 > 资讯 > 移动开发 >Android中传递对象的三种方法的实现
  • 173
分享到

Android中传递对象的三种方法的实现

方法对象Android 2022-06-06 04:06:21 173人浏览 八月长安
摘要

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

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

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

1.Serializable


public class Author implements Serializable{ 
  private int id; 
  private String name; 
  //... 
} 

public class Book implements Serializable{ 
  private String title; 
  private Author author; 
  //... 
} 

传递数据


 Book book=new Book();  
 book.setTitle("Java编程思想");  
 Author author=new Author();  
 author.setId(1);  
 author.setName("Bruce Eckel");  
 book.setAuthor(author);  
 Intent intent=new Intent(this,SecondActivity.class);  
 intent.putExtra("book",book);  
 startActivity(intent); 

接收数据


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

2.转化为JSON字符串


public class Author{ 
  private int id; 
  private String name; 
  //... 
} 

public class Book{ 
  private String title; 
  private Author author; 
  //... 
} 

传递数据


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

接收数据


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

3.使用Parcelable

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

describeContents方法。内容接口描述,默认返回0就可以; writeToParcel方法。将传递的数据打包到Parcel容器中。

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


public class Author implements Parcelable{ 
  private int id; 
  private String name; 
  //setter & getter... 
  @Override 
  public int describeContents() { 
    return 0; 
  } 
  @Override 
  public void writeToParcel(Parcel dest, int flags) { 
    //该方法将类的数据写入外部提供的Parcel中.即打包需要传递的数据到Parcel容器保存, 
    // 以便从parcel容器获取数据 
    dest.writeString(name); 
    dest.writeInt(id); 
  } 
  public static final Creator<Author> CREATOR=new Creator<Author>() { 
    @Override 
    public Author createFromParcel(Parcel source) { 
      //从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层。 
      Author author=new Author(); 
      author.setName(source.readString()); 
      author.setId(source.readInt()); 
      return author; 
    } 
    @Override 
    public Author[] newArray(int size) { 
      //创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。方法是供外部类反序列化本类数组使用。 
      return new Author[size]; 
    } 
  }; 
} 

public class Book implements Parcelable{ 
  private String title; 
  private Author author; 
  //setter & getter... 
  @Override 
  public int describeContents() { 
    return 0; 
  } 
  @Override 
  public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(title); 
    dest.writeParcelable(author,flags); 
  } 
  public static final Creator<Book> CREATOR=new Creator<Book>() { 
    @Override 
    public Book createFromParcel(Parcel source) { 
      Book book=new Book(); 
      book.setTitle(source.readString()); 
      book.setAuthor(source.<Author>readParcelable(Author.class.getClassLoader())); 
      return book; 
    } 
    @Override 
    public Book[] newArray(int size) { 
      return new Book[0]; 
    } 
  }; 
} 

传递数据


Book book=new Book(); 
book.setTitle("Java编程思想"); 
Author author=new Author(); 
author.setId(1); 
author.setName("Bruce Eckel"); 
book.setAuthor(author); 
Intent intent=new Intent(this,SecondActivity.class); 
intent.putExtra("book",book); 
startActivity(intent); 

接收数据


Book book=getIntent().getParcelableExtra("book"); 
Log.d(TAG,"book title->"+book.getTitle()); 
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进行注解,在传递数据的时候使用Parcels的wrap方法来包装成一个Parcelable对象。获取数据时用Parcels的unwrap方法来获取对象。


@Parcel 
public class Author { 
  int id; 
  String name; 
  //setter & getter... 
} 

@Parcel 
public class Book { 
  String title; 
  Author author; 
  //setter & getter 
} 

传递对象


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

接收对象


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

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

您可能感兴趣的文章:Android Intent传递对象的两种方法(Serializable,Parcelable)详细介绍Android 通过Intent使用Bundle传递对象详细介绍在Android中通过Intent使用Bundle传递对象的使用方法Android中Intent传递对象的3种方式详解详解Android中Intent传递对象给Activity的方法Android开发中Intent传递对象的方法分析Android中使用Intent在Activity之间传递对象(使用Serializable或者Parcelable)的方法Android传递Bitmap对象在两个Activity之间Android系列之Intent传递对象的几种实例方法Android 不同Activity间数据的传递 Bundle对象的应用


--结束END--

本文标题: Android中传递对象的三种方法的实现

本文链接: https://www.lsjlt.com/news/22341.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作