Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <pre><code>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&lt;Long&gt; expandedIds = new ArrayList&lt;Long&gt;(); for(int i=0; i &lt; 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&lt;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&lt;Long&gt; list) { long[] ret = new long[list.size()]; int i = 0; for (Long e : list) ret[i++] = e.longValue(); return ret; } } </code></pre> <p>Maybe someone has a better solution though.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload