[英]Pass bundle between three Fragments in Android
我有三个片段和一个对象,我需要先将它们传递给第二个片段,然后再传递给第三个片段。
第一:
ArrayList<Article> list = app.getDb().getArticles("userId", app.getUser(getActivity()).getId(),
"id", item.getId());
FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
ArticleFragment mFrag = new ArticleFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("article", list.get(0));
mFrag.setArguments(bundle);
t.replace(R.id.fragment_container, mFrag, ArticleFragment.TAG);
t.addToBackStack(ArticleFragment.TAG);
t.commit();
第二:
article = getArguments().getParcelable("article");
// some actions
ArticleEditFragment mFrag = new ArticleEditFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("article_edit", article);
mFrag.setArguments(bundle);
FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
t.replace(R.id.fragment_container, mFrag, ArticleEditFragment.TAG);
t.addToBackStack(null);
t.commit();
第三:
article = getArguments().getParcelable("article_edit");
//some actions with article
问题是当我完成第三个片段并返回第二个片段时,文章会发生变化。 第二个包含与我在第三个片段中编辑的对象相同的对象。 如何解决这个问题?
在你的First
片段已在束通过价值articles_list
和你的第二个你得到它article
应该是articles_list
。 因此,如下所示在您的Second
片段中更改它:
更改以下行:
article = getArguments().getParcelable("article");
至
article = getArguments().getParcelable("articles_list");
我的解决方法是在Article类中创建自己的“克隆”方法。
incomeArticle = getArguments().getParcelable("article_edit");
article = incomeArticle.cloneArticle();
/**
* Clone all field of current article to new
*
* @return new article
*/
public Article cloneArticle() {
Article article = new Article();
article.setName(this.name);
article.setId(this.id);
article.setUserId(this.userId);
article.setTypeId(this.typeId);
article.setPictureId(this.pictureId);
article.setAccess(this.access);
if (this.pictures != null) {
article.setPictures((ArrayList<String>) this.pictures.clone());
} else {
article.setPictures(new ArrayList<String>());
}
article.setComments(this.comments);
if (this.tags != null) {
article.setTags((ArrayList<String>) this.tags.clone());
} else {
article.setTags(new ArrayList<String>());
}
if (this.care != null) {
article.setCare((ArrayList<String>) this.care.clone());
} else {
article.setCare(new ArrayList<String>());
}
article.setFitLevel(this.fitLevel);
article.setRating(this.rating);
if (this.materials != null) {
article.setMaterials((ArrayList<ArticleMaterial>) this.materials.clone());
} else {
article.setMaterials(new ArrayList<ArticleMaterial>());
}
article.setBuyingDate(this.buyingDate);
article.setSize(this.size);
article.setArticul(this.articul);
article.setPrice(this.price);
article.setMadeInCountryId(this.madeInCountryId);
article.setCurrencyId(this.currencyId);
article.setCommentsCount(this.commentsCount);
article.setToWeight(this.toWeight);
article.setBrandId(this.brandId);
article.setChanged(this.changed);
article.setShowWeight(this.showWeight);
article.setCreated(this.created);
article.setDescription(this.description);
article.setIsLiked(this.isLiked);
article.setLikeRating(this.likeRating);
article.setFromWeight(this.fromWeight);
article.setGuid(this.guid);
return article;
}
在第二个片段中进行更改
article = new Article(getArguments().getParcelable("article"));
在第三段
article = new Article(getArguments().getParcelable("articles_list"));
并在您的文章pojo中创建一个构造函数
Article(Article A1)
{
value1=A1.value1;
value2=A1.value2;
value3=A1.value3;
...
}
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.