7

I have a SparseArray<myObject> and want to store it in bundle in onSaveInstanceState method in my activity and restore it in oncreate. I found putSparseParcelableArray method for put SparseArray in bundle and did this in onSaveInstanceState method:

bundle.putSparseParcelableArray("mySparseArray", mySparseArray);

But eclips shows this error:

The method putSparseParcelableArray(String, SparseArray<? extends Parcelable>) in the type Bundle is not applicable for the arguments (String, SparseArray<myObject>)

And the quick fix is casting argument mySparsArray to SparseArray<? extends Parcelable>, but if I do so and get it in onCreate method:

mySparseArray = (SparseArray<myObject>) savedInstanceState.getSparseParcelableArray("mySparseArray");

It gets this error:

Cannot cast from SparseArray<Parcelable> to SparseArray<myObject>

If this way is wrong, what is the solution for put mySparseArray in bundle? Any help would be much appreciated.

Ehsan
  • 400
  • 5
  • 15
  • What is `myObject`? Does it implement `Parcelable`? – Wenhui Feb 15 '13 at 17:21
  • It is a custom class that I defined and does not implement anything. Should it implements of Parcelable? – Ehsan Feb 15 '13 at 17:30
  • 2
    Yes, look at the arguments `putSparseParcelableArray`, it is `SparseArray extends Parcelable>`, so only the object that implements Parcelable can be put into the bundle. Do you need help for how to implement `Parcelable`, it is very straightforward. – Wenhui Feb 15 '13 at 17:33
  • 1
    [Here](http://stackoverflow.com/questions/7181526/example-of-implementing-parcelable) is one example. – Wenhui Feb 15 '13 at 17:35

2 Answers2

9

You can extend SparsArray to imlement a Serializable as use it:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import android.util.SparseArray;



/**
 * @author Asaf Pinhassi www.mobiledev.co.il
 * @param <E>
 *
 */
public class SerializableSparseArray<E> extends SparseArray<E> implements Serializable{

    private static final long serialVersionUID = 824056059663678000L;

    public SerializableSparseArray(int capacity){
        super(capacity);
    }

    public SerializableSparseArray(){
        super();
    }

    /**
     * This method is private but it is called using reflection by java
     * serialization mechanism. It overwrites the default object serialization.
     *
     * <br/><br/><b>IMPORTANT</b>
     * The access modifier for this method MUST be set to <b>private</b> otherwise {@link java.io.StreamCorruptedException}
     * will be thrown.
     *
     * @param oos
     *            the stream the data is stored into
     * @throws IOException
     *             an exception that might occur during data storing
     */
    private void writeObject(ObjectOutputStream oos) throws IOException {
        Object[] data = new  Object[size()];

        for (int i=data.length-1;i>=0;i--){
            Object[] pair = {keyAt(i),valueAt(i)}; 
            data[i] = pair;
        }
        oos.writeObject(data);
    }

    /**
     * This method is private but it is called using reflection by java
     * serialization mechanism. It overwrites the default object serialization.
     *
     * <br/><br/><b>IMPORTANT</b>
     * The access modifier for this method MUST be set to <b>private</b> otherwise {@link java.io.StreamCorruptedException}
     * will be thrown.
     *
     * @param oos
     *            the stream the data is read from
     * @throws IOException
     *             an exception that might occur during data reading
     * @throws ClassNotFoundException
     *             this exception will be raised when a class is read that is
     *             not known to the current ClassLoader
     */
    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        Object[] data = (Object[]) ois.readObject();
        for (int i=data.length-1;i>=0;i--){
            Object[] pair = (Object[]) data[i]; 
            this.append((Integer)pair[0],(E)pair[1]);
        }
        return;
    }


}
alcsan
  • 6,172
  • 1
  • 23
  • 19
Asaf Pinhassi
  • 15,177
  • 12
  • 106
  • 130
  • 1
    Any good reason the for loops works from back to front? Can't it just be a normal `for (int i = 0; i < data.length; i++)` ? – Diederik Sep 08 '14 at 09:06
  • 1
    @Diederik [Is it faster to count down than it is to count up?](http://stackoverflow.com/questions/2823043/is-it-faster-to-count-down-than-it-is-to-count-up) – Sufian Jan 29 '15 at 07:48
  • 2
    @Sufian I would prefer having more readable code, over micro optimisation. – Diederik Jan 30 '15 at 15:27
  • @Diederik me too, but since I don't find it any harder I don't complain. – Sufian Feb 02 '15 at 10:57
7

Your class should implement Parcelable and should have a static final member variable called CREATOR of type Parcelable.Creator<myObject>.

Adrian Taylor
  • 4,054
  • 2
  • 24
  • 26