How to pass ParcelableArrayList from Fragment to Fragment?

瘦欲@ 提交于 2020-12-15 05:30:45

问题


I have been trying to find the way to pass a ParcelableArrayList from one Fragment to Another and for some reason when I receive the ParcelableArrayList it's empty. I have checked many posts about this problem but I don't make it work.

This is my Activity that holds the Fragments in a FrameLayout:

package com.blumonpay.restaurant.customer.presentation.activities;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.WindowManager;

import com.blumonpay.android.mifel.restaurante.R;
import com.blumonpay.restaurant.customer.presentation.fragments.ShowPayMethodsFragment;

public class PayMethodsActivity extends AppCompatActivity {
    private Fragment showFragment;
    private Fragment addFragment;
   


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_pay_methods);

        showFragment = ShowPayMethodsFragment.getInstance(this);

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.frameLayout, showFragment)
                .commit();
        
    }

    public void switchFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.frameLayout, fragment)
                .commit();
    }

   
}

This is how I'm trying to pass the ArrayList in my first Fragment:

        Bundle dataBundle = new Bundle();
        dataBundle.putParcelableArrayList("cardsList", (ArrayList<? extends Parcelable>) list);
        dataBundle.putInt("position",position);
        ShowDetailedPayMethodFragment showDetailPayMethod = ShowDetailedPayMethodFragment.getInstance(context);
        showDetailPayMethod.setArguments(dataBundle);
        ((PayMethodsActivity)context).switchFragment(showDetailPayMethod);

As you can see I'm also passing an int value which I can receive with no problem at all.

Then, this is the second Fragment where I'm receiving the data (ParcelableArrayList and the int value):

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_view_detailed_pay_method, container, false);

        if (this.getArguments()!=null) {
            cardsList = this.getArguments().getParcelableArrayList("cardsList");
            positionList = this.getArguments().getInt("position");
        }
        
        cardsBannerAdapter = new CustomCardsBannerAdapter(context, cardsList);
        cardsViewPager.setAdapter(cardsBannerAdapter);

        initSlider();
        return v;
    }

For some reason the List is empty when I get to this point, the only value I'm getting is this.getArguments().getInt("position"); (int value)

And finally this is my POJO class Cards:

package com.blumonpay.restaurant.customer.domain.model;

import android.os.Parcel;
import android.os.Parcelable;

public class Cards implements Parcelable {

    private String cardNumber;
    private String cardType;
    private String cardBrand;
    private String cardExpirationDate;
    private String cardHolderName;

    public Cards(String cardNumber, String cardType, String cardBrand, String cardExpirationDate, String cardHolderName) {
        this.cardNumber = cardNumber;
        this.cardType = cardType;
        this.cardBrand = cardBrand;
        this.cardExpirationDate = cardExpirationDate;
        this.cardHolderName = cardHolderName;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getCardType() {
        return cardType;
    }

    public void setCardType(String cardType) {
        this.cardType = cardType;
    }

    public String getCardBrand() {
        return cardBrand;
    }

    public void setCardBrand(String cardBrand) {
        this.cardBrand = cardBrand;
    }

    public String getCardExpirationDate() {
        return cardExpirationDate;
    }

    public void setCardExpirationDate(String cardExpirationDate) {
        this.cardExpirationDate = cardExpirationDate;
    }

    public String getCardHolderName() {
        return cardHolderName;
    }

    public void setCardHolderName(String cardHolderName) {
        this.cardHolderName = cardHolderName;
    }



    protected Cards(Parcel in) {
        cardNumber = in.readString();
        cardType = in.readString();
        cardBrand = in.readString();
        cardExpirationDate = in.readString();
        cardHolderName = in.readString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(cardNumber);
        dest.writeString(cardType);
        dest.writeString(cardBrand);
        dest.writeString(cardExpirationDate);
        dest.writeString(cardHolderName);
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<Cards> CREATOR = new Parcelable.Creator<Cards>() {
        @Override
        public Cards createFromParcel(Parcel in) {
            return new Cards(in);
        }

        @Override
        public Cards[] newArray(int size) {
            return new Cards[size];
        }
    };
}

Any clue of what am I missing?


回答1:


Tried the same in kotlin, with the Cards.java intact. The list gets passed. Here is the snippet. Could you try and compare?

In first fragment as an example:

val item = Cards("gfhfhfhfh", "", "", "", "")
val list = ArrayList<Cards>()
list.add(item

Passing it as: (no explicit cast)

bundle.putParcelableArrayList("list",list)

In the next fragment:

val l = arguments?.getParcelableArrayList<Cards>("list")



回答2:


According to this post, How to marshall and unmarshall Parcelable, Parcelable object is not stable which might well be the reason that you cannot pass it from one activity to another. I faced similar problem recently and used the same post to solve my problem. Basically in steps: 1. You need to marshall the Parcelable object in the first Activity as :

byte[] bytes = marshall(parcelableObj);

intentOpenSecondActivity.putExtra(SOME_KEY, bytes);

2. You need to unmarshall it in the second Activity as :

byte[] bytes = getIntent().getExtras().getByteArray(SOME_KEY);

parcelableObj = unmarshall(bytes, ParcelableObject.CREATOR);


来源:https://stackoverflow.com/questions/62013958/how-to-pass-parcelablearraylist-from-fragment-to-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!