Android Fragment 間物件傳遞

秋楓發表於2015-10-24
由於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進行設定和獲取。

相關文章