Sonhja
Sonhja

Reputation: 8458

Share large data amount between activities

I have a parcelable class that is trying to send some camera preview information. So I need to send several byte[] data from one activity to another. I'm using Parcelable with this code:

public class CameraResult implements Parcelable
{   
    private byte[] imagesList; 

    public CameraResult() {}

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByteArray(imagesList);
    }

    public byte[] getImageList() {
        return imagesList;
    }

    public void setImageList(byte[] results) {
        this.imagesList = results;
    }


public static final Parcelable.Creator<CameraResult> CREATOR = new Creator<CameraResult>() {

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

    @Override
    public CameraResult createFromParcel(Parcel source) {
        CameraResult toReturn = new CameraResult();

        int imageListSize = source.readInt();
        if (imageListSize != 0)
        {
            byte[] tempImageList = new byte[imageListSize];
            source.readByteArray(tempImageList);
            toReturn.setImageList(tempImageList);
        }

      return toReturn;
    }
};
}

This Parcelable class is working fine. But when I try to send a higher amount of data, the communication Intent is not working, as I exceed the maximum data size.

I read this link and this other link with some supposed ways to solve my problem. But I would like to know if it's possible to send higher amounts of data using Parcelable, and if not, which of the mentioned ways is the most adequate one.

Thank you.

Upvotes: 0

Views: 1191

Answers (1)

Juanjo Vega
Juanjo Vega

Reputation: 1440

Use a singleton or static class to store the shared data.


EDIT:

According to my experience, that's the less painful way to do such a thing, but, also, on the first link you posted it says:

There are several suggestions to achieve this, through defining global variables, static fields, member variables, etc. Having tweaked those ones a bit to get clean encapsulated code, I decided to implement it using Java enum.

It uses a enum as DataHolder:

private enum DataHolder {
    INSTANCE;

    private List<Object> mObjectList;

    public static boolean hasData() {
        return INSTANCE.mObjectList != null;
    }

    public static void setData(final List<Object> objectList) {
        INSTANCE.mObjectList = objectList;
    }

    public static List<Object> getData() {
        final List<Object> retList = INSTANCE.mObjectList;
        INSTANCE.mObjectList = null;
        return retList;
    }
}

I personally prefer to implement a Singleton class as code seems to be more tidy in a different class (I'm a bit obsessed with that), but that's basically the same. Any other solution could be fine as well.

Upvotes: 2

Related Questions