user3576118
user3576118

Reputation: 375

Android getParcelableExtra returns null

I am currently trying to pass some data from my fragment to an activity. I am passing some parameter, and also an arraylist of a model class. I already make sure that the arraylist before I passed it is not null, since I am able to print out the value in log. But it become null in the called activity.

Here is the model class:

public class Product implements Parcelable {

String code, id, status, is_expired;

public Product() {}

public Product(String code, String id, String status, String is_expired) {
    this.code = code;
    this.id = id;
    this.status = status;
    this.is_expired = is_expired;
}

public Product(Parcel in) {
    code = in.readString();
    id = in.readString();
    status = in.readString();
    is_expired = in.readString();
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getIs_expired() {
    return is_expired;
}

public void setIs_expired(String is_expired) {
    this.is_expired = is_expired;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(code);
    dest.writeString(id);
    dest.writeString(status);
    dest.writeString(is_expired);
}

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

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

Here is the caller:

intent.putExtra("product", productList);

Here is how retrieve the data:

ArrayList<Product> productList = getIntent().getParcelableExtra("product");

What am I doing wrong here?

Upvotes: 0

Views: 1625

Answers (1)

KeLiuyue
KeLiuyue

Reputation: 8237

Try this .

intent.putParcelableArrayListExtra("product", productList);

And next Activity .

ArrayList<Product> productList = getIntent().getParcelableArrayListExtra("product");

Upvotes: 5

Related Questions