Last active
June 26, 2017 16:30
-
-
Save kaushikgopal/9eea148a2188dc58fe37 to your computer and use it in GitHub Desktop.
Parcelable version of Android's SparseArray. Apache 2 licensed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Intent; | |
import android.os.Bundle; | |
import android.os.Parcel; | |
import android.os.Parcelable; | |
import android.util.SparseArray; | |
import junit.framework.TestCase; | |
public class ParcelableSparseArray | |
extends SparseArray<Object> | |
implements Parcelable { | |
public static final Parcelable.Creator<ParcelableSparseArray> CREATOR = new Creator<ParcelableSparseArray>() { | |
@Override | |
public ParcelableSparseArray createFromParcel(Parcel source) { | |
return new ParcelableSparseArray(source); | |
} | |
@Override | |
public ParcelableSparseArray[] newArray(int size) { | |
return new ParcelableSparseArray[size]; | |
} | |
}; | |
private SparseArray<Object> _sparseArray; | |
public ParcelableSparseArray() { | |
super(); | |
} | |
// ----------------------------------------------------------------------- | |
// Parcelable implementation | |
private ParcelableSparseArray(Parcel in) { | |
_sparseArray = in.readSparseArray(ParcelableSparseArray.class.getClassLoader()); | |
} | |
public Object[] values() { | |
Object[] values = new Object[size()]; | |
for (int i = 0; i < size(); i++) { | |
values[i] = valueAt(i); | |
} | |
return values; | |
} | |
@Override | |
public int describeContents() { | |
return 0; | |
} | |
@Override | |
public void writeToParcel(Parcel out, int _) { | |
out.writeSparseArray(_sparseArray); | |
} | |
// ----------------------------------------------------------------------- | |
// Tests | |
public class ParcelableSparseArrayTest | |
extends TestCase { | |
public static final String TEST_STRING = "Test String"; | |
public static final int TEST_INTEGER = 3; | |
public static final Event TEST_EVENT = new Event(); | |
private ParcelableSparseArray _testSparserray; | |
public void test_IsParcelable() { | |
Bundle testBundle = new Bundler("testSparseArray", _testSparserray).build(); | |
Intent testIntent = new Intent("someAction"); | |
testIntent.putExtras(testBundle); | |
ParcelableSparseArray testSource = testIntent.getParcelableExtra("testSparseArray"); | |
assertThat(testSource.get(1)).isEqualTo(TEST_STRING); | |
assertThat(testSource.get(3)).isEqualTo(TEST_INTEGER); | |
assertThat(testSource.get(5)).isEqualTo(TEST_EVENT); | |
} | |
public void test_ValuesReturnsObjectArray() { | |
Object[] testObjectWithValues = _testSparserray.values(); | |
assertThat(testObjectWithValues.length).isEqualTo(3); | |
assertThat(testObjectWithValues[0]).isEqualTo(TEST_STRING); | |
assertThat(testObjectWithValues[1]).isEqualTo(TEST_INTEGER); | |
assertThat(testObjectWithValues[2]).isEqualTo(TEST_EVENT); | |
} | |
@Override | |
protected void setUp() throws Exception { | |
super.setUp(); | |
_testSparserray = new ParcelableSparseArray(); | |
_testSparserray.put(1, TEST_STRING); | |
_testSparserray.put(3, TEST_INTEGER); | |
_testSparserray.put(5, TEST_EVENT); | |
} | |
} | |
} |
it doesn't work
Haven't revisited this in sometime but are you folks saying the test also doesn't run successfully?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not work when process is killed.
_sparseArray = in.readSparseArray(ParcelableSparseArray.class.getClassLoader());
is always empty. But it can store and restore other values likeint
.