
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,book); startActivity(intent); 接收数据
Book book=getIntent().getParcelableExtra("book"); 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,Parcels.wrap(book)); startActivity(intent); 接收对象
Book book= Parcels.unwrap(getIntent().getParcelableExtra("book")); Log.d(TAG,"book author name->"+book.getAuthor().getname()); 除了Parceler之外,还有如auto-parcel,ParcelableCodeGenerator,ParcelableGenerator等第三方库,这里我将不进行讲解,有兴趣的朋友,可以自行研究。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android中传递对象的三种方法的实现全部内容,希望文章能够帮你解决Android中传递对象的三种方法的实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)