android - Save and restore expanded/collapsed state of an ExpandableListActivity -
i have expandablelistactivity (using simplecursortreeadapter) starts activity when user clicks on child-element. when pressing back-button in new activity list-items collapsed again. how save expanded state of expandablelistactivity , restore again.
i tried implemented onsaveinstancestate() , onrestoreinstancestate() 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. tried restore state in oncreate() method isn't called well:
if (savedinstancestate != null) { parcelable liststate = savedinstancestate.getparcelable("liststate"); getexpandablelistview().onrestoreinstancestate(liststate); }
unfortunately expanded state resets whenever focus lost , oncreate() not called when gets focus again onstart(). workaround manually store ids of expanded items , expand them in onstart() again. implemented subclass of expandablelistactivity reuse 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; < 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) { (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) { (long l : array) { if (l == element) { return true; } } return false; } private static long[] tolongarray(list<long> list) { long[] ret = new long[list.size()]; int = 0; (long e : list) ret[i++] = e.longvalue(); return ret; } }
maybe has better solution though.
Comments
Post a Comment