简体   繁体   中英

Null bundle when passing from activity to fragment

When trying to pass a custom object from an activity to a fragment by using a bundle, I kept getting a null bundle when I tried to retrieve it in the fragment. I am using parcels since they are the only option for custom arraylists. Any help appreciated!

To show that my class is parcelable:

public class Exercise implements Parcelable{

    private String mName;
    private String mMuscleGroup;
    private String mType;
    private String mEquipment;
    private float mWeight;
    private int mReps;
    private int mSet;

    public Exercise(String name, String MuscleGroup, String type, String equipment, float weight, int reps, int set) {
        mName = name;
        mMuscleGroup = MuscleGroup;
        mType = type;
        mEquipment = equipment;
        mWeight = weight;
        mReps = reps;
        mSet = set;
    }

    private Exercise(Parcel in) {
        mName = in.readString();
        mMuscleGroup = in.readString();
        mType = in.readString();
        mEquipment = in.readString();
        mWeight = in.readFloat();
        mReps = in.readInt();
        mSet = in.readInt();
    }

    public static final Creator<Exercise> CREATOR = new Creator<Exercise>() {
        @Override
        public Exercise createFromParcel(Parcel in) {
            return new Exercise(in);
        }

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

    public String getName() {
        return mName;
    }

    public String getMuscleGroup() {
        return mMuscleGroup;
    }

    public String getType() {
        return mType;
    }

    public String getEquipment() {
        return mEquipment;
    }

    public float getWeight() { return mWeight; }

    public int getReps() { return mReps; }

    public int getSet() { return mSet; }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(mName);
        parcel.writeString(mMuscleGroup);
        parcel.writeString(mType);
        parcel.writeString(mEquipment);
        parcel.writeFloat(mWeight);
        parcel.writeInt(mReps);
        parcel.writeInt(mSet);
    }
}
'''

Activity I am passing bundle from:

@Override
protected void onPause() {
     // send ArrayList of exercises to WorkoutsFragment
     WorkoutsFragment workoutsFragment = new WorkoutsFragment();
     Bundle bundle = new Bundle();
     bundle.putSerializable("exercise", exercise);
     workoutsFragment.setArguments(bundle);

     // clear exercise ArrayList

     super.onPause();
 }

Fragment receiving bundle:

 Bundle bundle = this.getArguments();
 if(bundle != null) {
     exercise = bundle.getParcelableArrayList("exercise");
 }

Model class

public class SomeModelClass implements Serializable {

    private String someVar;

    public PurchaseData(String someVar) {
        this.someVar = someVar;
    }

    public String getSomeVar() {
        return productId;
    }

    public void setSomeVar(String someVar) {
        this.someVar = someVar;
    }
}

Passing it to Frag

@Override
protected void onPause() {
    // send ArrayList of exercises to WorkoutsFragment
    WorkoutsFragment workoutsFragment = new WorkoutsFragment();
    Bundle bundle = new Bundle();
    bundle.putSerializable("exercise", yourObject);
    workoutsFragment.setArguments(bundle);

    super.onPause();
}

Retrieving it

 if (getArguments().getSerializable("exercise") != null) {
        exercise = (YourObjectClass) getArguments().getSerializable("exercise");
    }

You need to use bundle.putParcelableArrayList to put Parcelable list objects, not putSerializable

If the types don't match, you get null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM