I tried the solution creating SerializableSparseArray extending SparseArray making it possible to put a SparseArray into bundle via Bundle.putSerializable call.
But I found I can't obtain the saved object from bundle in onRestoreInstanceState. Digging into the issue I found that savedInstanceState.getSerializable(KEY_TO_STRING_SPARSE_ARRAY) == null.
Then, trying to inspect savedInstanceState.get(KEY_TO_STRING_SPARSE_ARRAY) and suprisingly got a SparseArray<String> not SerializableSparseArray<String>. Finally I'm using savedInstanceState.getSparseParcelableArray(KEY_TO_STRING_SPARSE_ARRAY) to obtaining SparseArray back from a bundle.
Then, using java reflection to save a SparseArray<String> to bundle directly without extending with Serializable or Parcelable interface. It's a little bit dirty but I think you can make your own utility function hiding the following detail implementation.
try {
// FIXME WTF
Method m = Bundle.class.getMethod("putSparseParcelableArray", String.class, SparseArray.class);
m.invoke(savedInstanceState, KEY_TO_STRING_SPARSE_ARRAY, mSparseStringArray);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
Log.e(TAG, "exception", e);
}
I've tested the code and it works on Android 4.4.4. And I'd like to know if it's safe to use the implementation in other SDK implementations.