由于Activity相对较为笨重,因而在日常的开发中很多时候会用到Fragment。然而Activity之间是通过Intent进行数据的传递,那Fragment是通过什么来进行传递的呢?Fragment之间又是通过什么方式来进行对象的传递呢?
Android控件之间传递(Activity和Activity间传递对象、Fragment和Fragment间传递对象)对象有两种实现方式。一种是实现Java提供的Serializable接口,另一种是实现android提供的Parcelable接口。两者之间各有优势;实现Serializable接口的话最简单,直接添加接口的实现即可,但是效率较低;实现Parcelable的话效率较高,但需要自己实现Parcelable的方法。下面看下两种方法的具体实现:
原有Article类
package com.example.news.andoridnewsapp; public class Article { private String title; private String linkmd5; private String link; private String desc; public String getLinkmd5() { return linkmd5; } public void setLinkmd5(String linkmd5) { this.linkmd5 = linkmd5; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
一、实现Serializable,只要在 public class Article后面添加接口的实现(implements Serializable )即可
package com.example.news.andoridnewsapp; public class Article implements Serializable { private String title; private String linkmd5; private String link; private String desc; public String getLinkmd5() { return linkmd5; } public void setLinkmd5(String linkmd5) { this.linkmd5 = linkmd5; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
Fragment1进行参数传递
ArticleConententFragment fragment = new ArticleConententFragment(); Bundle args = new Bundle(); args.putSerializable(ARG_ARTICLE, article); fragment.setArguments(args);
Fragment2进行参数的接收
(Article) getArguments().getSerializable(ARG_ARTICLE);
二、实现Parcelable,如果通过Parcelable的方式进行实现的话,需要实现Parcelable接口的方法describeContents(此方法一般不用修改)、writeToParcel(主要是此方法)和创建一个Creator属性。
package com.example.news.andoridnewsapp; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; //public class Article implements Serializable { public class Article implements Parcelable { private String title; private String linkmd5; private String link; private String desc; public String getLinkmd5() { return linkmd5; } public void setLinkmd5(String linkmd5) { this.linkmd5 = linkmd5; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Override public String toString() { return this.title; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { Bundle bundle = new Bundle(); bundle.putString("title", getTitle()); bundle.putString("desc", getDesc()); bundle.putString("link", getLink()); bundle.putString("linkmd5", getLinkmd5()); dest.writeBundle(bundle); } public static final Creator<Article> CREATOR = new Creator<Article>() { @Override public Article createFromParcel(Parcel source) { Bundle bundle = source.readBundle(); Article article = new Article(); article.setTitle(bundle.getString("title")); article.setDesc(bundle.getString("desc")); article.setLink(bundle.getString("link")); article.setLinkmd5(bundle.getString("linkmd5")); return article; } @Override public Article[] newArray(int size) { return new Article[size]; } }; }
Fragment1进行参数传递
ArticleConententFragment fragment = new ArticleConententFragment();
Bundle args = new Bundle(); //args.putSerializable(ARG_ARTICLE, article); args.putParcelable(ARG_ARTICLE, article); fragment.setArguments(args);
Fragment2进行参数接收
// mArticle = (Article) getArguments().getSerializable(ARG_ARTICLE); mArticle = (Article) getArguments().getParcelable(ARG_ARTICLE);
这样便可方便地实现对象参数的传递。其实传递的是一个Bundle对象,如果实现的是Serializable接口,则通过Bundle的putSerializable和getSerializable进行设置和获取;如果实现的是Parceable接口,则通过putParceable和getParceable进行设置和获取。