0

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");
 }
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
Paul Lim
  • 1
  • 1

1 Answers1

0

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

If the types don't match, you get null

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I'm an idiot lol. I've changed it to putParcelableArrayList, but still getting null tho :/ – Paul Lim Oct 27 '19 at 16:08
  • Well, you're creating a new Fragment onPause of the Activity, but never actually using that Fragment anywhere – OneCricketeer Oct 27 '19 at 16:14
  • How would I pass the bundle to the fragment then? – Paul Lim Oct 27 '19 at 17:15
  • You're passing it correctly to a Fragment, but it's not clear to me if you already have a Fragment loaded an displayed, and you wouldn't define that in onPause when the Activity is about to be destroyed anyway – OneCricketeer Oct 27 '19 at 17:29
  • I want to pass the arraylist from the activity when it is exited and to a fragment that is not yet displayed. Is this possible? (Thanks for your help btw) – Paul Lim Oct 27 '19 at 18:52
  • But if the Activity exits, the Fragment will be destroyed, so passing the Arraylist seems pointless then – OneCricketeer Oct 27 '19 at 18:56
  • Is there a way I can save this fragment to be opened when I click on an item? – Paul Lim Oct 27 '19 at 19:01
  • You could define `private WorkoutsFragment workoutsFragment` as a field, initialize in an onClick, yes. But that still doesn't solve the onPause issue, and you shouldn't need to declare a Fragment more than once... You can load it in onCreate, then use interfaces to pass data from the Activity into a Fragment (although the Fragment should probably be creating Exercise objects, not the Activity) – OneCricketeer Oct 27 '19 at 19:06