When a config change happens, my ListView Checkbox states get lost, which I understand why. I try to implement
public void onSaveInstanceState(final Bundle outState)
in one of my Fragments. So I'm just wondering what's the easiest way to store my SparseBooleanArray in the outState.
Also, I'm a bit confused, as ListView has the method:
getListView().getCheckedItemPositions();
What's this good for?
I tried the solution creating
SerializableSparseArray
extendingSparseArray
making it possible to put aSparseArray
into bundle viaBundle.putSerializable
call.But I found I can't obtain the saved object from bundle in
onRestoreInstanceState
. Digging into the issue I found thatsavedInstanceState.getSerializable(KEY_TO_STRING_SPARSE_ARRAY) == null
.Then, trying to inspect
savedInstanceState.get(KEY_TO_STRING_SPARSE_ARRAY)
and suprisingly got aSparseArray<String>
notSerializableSparseArray<String>
. Finally I'm usingsavedInstanceState.getSparseParcelableArray(KEY_TO_STRING_SPARSE_ARRAY)
to obtainingSparseArray
back from a bundle.Then, using java reflection to save a
SparseArray<String>
to bundle directly without extending withSerializable
orParcelable
interface. It's a little bit dirty but I think you can make your own utility function hiding the following detail implementation.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.
I don't see any answers to the last part of the question:
Assuming you're using checkboxes in conjunction with Action Mode, the Action Mode itself will store the state of checked positions. In the normal mode, a press-and-hold will highlight an item, enable action mode, and then allow the user to tap other items to also check them.
What happens on an orientation change? Action mode is resumed and the highlights are kept. Your checkboxes aren't, but clearly the data of what is checked is.
After an orientation change,
onCreateActionMode()
is called. In this method (but not before),getListView().getCheckedItemPositions()
will return theSparseBooleanArray
you're probably looking for.The nifty part about this is if this is your only use case for the
SparseBooleanArray
, you need not even worry about storing it yourself. You can retrieve it from the system which is already storing it across the orientation change.That's what
getListView().getCheckedItemPositions()
is good for.Here is my solution. Just a simple wrapper that can be used to serialise a
SparseBooleanArray
.I then add the object to a bundle like so:
In my case, I ended up doing it by implementing a Parcelable wrapper around the SparseBooleanArray, like this:
This allows you to save and load your SparseBooleanArray by just doing:
Just my .02€
You could either invent some serialization scheme for that data structure or switch to a HashSet and store only the list index positions in it which are checked. Since HashSet is serializable you can just put it into the instance state Bundle.
I would create a
Parcelable
holder class, using the native methods fromParcel
in order to serialize/deserialize theSparseBooleanArray
: