我有三个片段和一个对象,我需要从第一个传递到第二个然后传递到第三个片段。
首先:
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
当我完成第三个片段并返回第二个片段时,问题是文章改变了。第二个包含我在第三个片段中编辑的相同对象。如何解决这个问题?
答案 0 :(得分:0)
在您的First
片段中,您已将包中的值作为articles_list
传递,而在您的第二个中,您将其article
作为articles_list
获得。因此,请在Second
片段中将其更改为如下所示:
更改以下行:
article = getArguments().getParcelable("article");
要
article = getArguments().getParcelable("articles_list");
答案 1 :(得分:0)
我的解决方法是在Article class中创建自己的'Clone'方法。
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;
}
答案 2 :(得分:-1)
进行更改 在第二个片段
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;
...
}
希望这有帮助