AdamMc331
AdamMc331

Reputation: 16710

Unable to read a parcelable object within another parcelable object

I have a class for Prescriptions which has fields for Medication, Doctor, and Pharmacy. Each of those classes implement Parcelable so that they can be passed inside of a Bundle.

For Medication, Doctor, and Pharmacy, I don't have any trouble. However, things get a little trickier for Pharmacy as it has fields that are objects which also implement parcelable. In order to write the object, I used the following code which I gained from this question:

/**
 * Bundles all the fields of a pharmacy object to be passed in a `Bundle`.
 * @param dest The parcel that will hold the information.
 * @param flags Any necessary flags for the parcel.
 */
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeParcelable(getMedication(), 0);
    dest.writeParcelable(getDoctor(), 0);
    dest.writeParcelable(getPharmacy(), 0);
    dest.writeInt(getQuantity());
    dest.writeSerializable(getStartDate());
    dest.writeString(getNotes());
    dest.writeString(getInstructions());
}

And the Creator used to read the Prescription is written like this:

public static final Creator<Prescription> CREATOR = new Creator<Prescription>() {
    @Override
    public Prescription createFromParcel(Parcel source) {
        return new Prescription(
                source.readLong(), // Id
                (Medication) source.readParcelable(Medication.class.getClassLoader()), // Medication
                (Doctor) source.readParcelable(Doctor.class.getClassLoader()), // Doctor
                (Pharmacy) source.readParcelable(Pharmacy.class.getClassLoader()), // Pharmacy
                source.readInt(), // Quantity
                (Date) source.readSerializable(), // Start Date
                source.readString(), // Notes
                source.readString() // Instructions
        );
    }

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

When I try to read a Prescription object from a Bundle, it returns a Prescription object with null values for Med/Doctor/Pharm, and really obscure Id and Quantity values. I do not know why. What would cause these values to be null?

Here is the implementation:

// Inside the NewPrescriptionActivity
Intent data =  new Intent();
data.putExtra(PrescriptionBinderActivity.ARG_PRESCRIPTION, prescription);

setResult(RESULT_OK, data);

// Inside the Activity that calls it.
if(requestCode == ADD_SCRIPT_REQUEST && resultCode == RESULT_OK){
    Prescription p = data.getParcelableExtra(ARG_PRESCRIPTION);
    mAdapter.addPrescription(p);
}else{
    super.onActivityResult(requestCode, resultCode, data);
}

Again, I have used the same approach on the other classes without any problem, but this does not work for Prescription. I suspect because it has Parcelable fields.

Upvotes: 0

Views: 379

Answers (1)

JDenais
JDenais

Reputation: 3016

You are not adding the id field to the Parcel.

Modify the first line of your writeToParcel() method and add :

dest.writeLong(getId());

Because of that, the entire reading is wrong.

Upvotes: 1

Related Questions