簡體   English   中英

在Android中的三個片段之間傳遞捆綁

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM