Question

I am passing a parcelable object from fragment to fragment, and the the resulting object is OK.

But in the last fragment I pass the parcelable object into an IntentService, and when I get that object all Parcelable objects that are in that object are null, and other values are random.

This is how I get my parcelable object in the last fragment

public static ValidInstallmentsFragment newInstance(CheckoutRequest checkoutRequest, Card card){

    Bundle bundle = new Bundle();
    bundle.putParcelable(BundleHelper.CHECKOUT_REQUEST, checkoutRequest);

    ValidInstallmentsFragment validInstallmentsFragment = new ValidInstallmentsFragment();
    validInstallmentsFragment.setArguments(bundle);

    return validInstallmentsFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    checkoutRequest = (CheckoutRequest) getArguments().getParcelable(BundleHelper.CHECKOUT_REQUEST);
}

After that I send the Intent with parcelable data to an IntentService

Bundle bundle = new Bundle();
bundle.putParcelable(BundleHelper.SEND_SECURE_3D_REQUEST, sendSecure3DRequest);
bundle.putParcelable(BundleHelper.CHECKOUT_REQUEST, checkoutRequest);

mCardPaymentServiceIntent = new Intent(getActivity(), CardPaymentIntentService.class);
mCardPaymentServiceIntent.putExtras(bundle);
getActivity().startService(mCardPaymentServiceIntent);

In the IntentService I get that object in the onHandleIntent method:

@Override
protected void onHandleIntent(Intent intent) {
    checkoutRequest = (CheckoutRequest) intent.getExtras().getParcelable(BundleHelper.CHECKOUT_REQUEST);
}

But in the IntentService i try to get an object BillingInformation from the CheckoutRequest object, but it returns null. Also there are a few other object in the Checkout Request object but all of them are null. Also normal int and String variables return some random values. And YES all of them are implementing the Parcelable interface.

This is my CheckoutRequest class:

public class CheckoutRequest implements Parcelable {

public CheckoutRequest() {
}

private String email;

private List<Product> products;

private BillingInformation billingInformation;

private ShippingMethod shippingMethod;

private int paymentMethod;

private float amount;

private String hashCode;

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public List<Product> getProducts() {
    return products;
}

public void setProducts(List<Product> products) {
    this.products = products;
}

public BillingInformation getBillingInformation() {
    return billingInformation;
}

public void setBillingInformation(BillingInformation billingInformation) {
    this.billingInformation = billingInformation;
}

public ShippingMethod getShippingMethod() {
    return shippingMethod;
}

public void setShippingMethod(ShippingMethod shippingMethod) {
    this.shippingMethod = shippingMethod;
}

public int getPaymentMethod() {
    return paymentMethod;
}

public void setPaymentMethod(int paymentMethod) {
    this.paymentMethod = paymentMethod;
}

public float getAmount() {
    return amount;
}

public void setAmount(float amount) {
    this.amount = amount;
}

public String getHashCode() {
    return hashCode;
}

public void setHashCode(String hashCode) {
    this.hashCode = hashCode;
}

public static final Parcelable.Creator<CheckoutRequest> CREATOR = new Parcelable.Creator<CheckoutRequest>() {
    public CheckoutRequest createFromParcel(Parcel in) {
        return new CheckoutRequest(in);
    }

    public CheckoutRequest[] newArray(int size) {
        return new CheckoutRequest[size];
    }
};

private CheckoutRequest(Parcel in) {

    email = in.readString();
    products = new ArrayList<Product>();
    in.readTypedList(products, Product.CREATOR);
    shippingMethod = in.readParcelable(ShippingMethod.class.getClassLoader());
    billingInformation = in.readParcelable(BillingInformation.class.getClassLoader());
    paymentMethod = in.readInt();
    amount = in.readFloat();
    hashCode = in.readString();

}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(email);
    dest.writeList(products);
    dest.writeParcelable(shippingMethod, flags);
    dest.writeParcelable(billingInformation, flags);
    dest.writeInt(paymentMethod);
    dest.writeFloat(amount);
    dest.writeString(hashCode);

}

}

Was it helpful?

Solution

You use readTypedList.

Read into the given List items containing a particular object type that were written with writeTypedList(List) at the current dataPosition(). The list must have previously been written via writeTypedList(List) with the same object type.

So you must use writeTypedList also, instead of:

dest.writeList(products);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top