I have an ExpandableListActivity (using a SimpleCursorTreeAdapter) which starts another activity when the user clicks on a child-element. When pressing the back-button in the new activity all the list-items are collapsed again. How do I save the expanded state of the ExpandableListActivity and restore it again.
I already tried to implemented onSaveInstanceState() and onRestoreInstanceState() like this...
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Parcelable listState = getExpandableListView().onSaveInstanceState();
outState.putParcelable("ListState", listState);
}
@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
Parcelable listState = state.getParcelable("ListState");
getExpandableListView().onRestoreInstanceState(listState);
}
...but onRestoreInstanceState() gets never called. I also tried to restore the state in the onCreate() method but it isn't called as well:
if (savedInstanceState != null) {
Parcelable listState = savedInstanceState.getParcelable("ListState");
getExpandableListView().onRestoreInstanceState(listState);
}
Unfortunately the expanded state always resets whenever the focus is lost and onCreate() is not called when it gets the focus again but onStart(). So my workaround for now is to manually store the IDs of all expanded items and expand them in onStart() again. I implemented a subclass of ExpandableListActivity to reuse the behavior.
public class PersistentExpandableListActivity extends ExpandableListActivity {
private long[] expandedIds;
@Override
protected void onStart() {
super.onStart();
if (this.expandedIds != null) {
restoreExpandedState(expandedIds);
}
}
@Override
protected void onStop() {
super.onStop();
expandedIds = getExpandedIds();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
this.expandedIds = getExpandedIds();
outState.putLongArray("ExpandedIds", this.expandedIds);
}
@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
long[] expandedIds = state.getLongArray("ExpandedIds");
if (expandedIds != null) {
restoreExpandedState(expandedIds);
}
}
private long[] getExpandedIds() {
ExpandableListView list = getExpandableListView();
ExpandableListAdapter adapter = getExpandableListAdapter();
if (adapter != null) {
int length = adapter.getGroupCount();
ArrayList<Long> expandedIds = new ArrayList<Long>();
for(int i=0; i < length; i++) {
if(list.isGroupExpanded(i)) {
expandedIds.add(adapter.getGroupId(i));
}
}
return toLongArray(expandedIds);
} else {
return null;
}
}
private void restoreExpandedState(long[] expandedIds) {
this.expandedIds = expandedIds;
if (expandedIds != null) {
ExpandableListView list = getExpandableListView();
ExpandableListAdapter adapter = getExpandableListAdapter();
if (adapter != null) {
for (int i=0; i<adapter.getGroupCount(); i++) {
long id = adapter.getGroupId(i);
if (inArray(expandedIds, id)) list.expandGroup(i);
}
}
}
}
private static boolean inArray(long[] array, long element) {
for (long l : array) {
if (l == element) {
return true;
}
}
return false;
}
private static long[] toLongArray(List<Long> list) {
long[] ret = new long[list.size()];
int i = 0;
for (Long e : list)
ret[i++] = e.longValue();
return ret;
}
}
Maybe someone has a better solution though.