title: Android: ListView, at the same time remembering and forgetting its getCheckedItemPosition after rotation/configuration change
body: <p>I really can't make this up so I'd be thankful for any hint. I must make some mistake here (4.1.2).</p>
  
  <p>I have an <code>Activity</code> which, in <code>onCreate()</code>, sets up a subclassed <code>ArrayAdapter</code> for <code>ListView</code> items which render as a <code>Checkable</code> <code>ViewGroup</code>.</p>
  
  <p>The <code>Activity</code> already utilizes a <code>NonConfiguration</code> mechanism to re-build the Adapter upon orientation change. However, it's currently not storing the <code>ListView</code>'s <code>getCheckedItemPosition()</code> because I feel it shouldn't be necessary (details below).</p>
  
  <p>Interestingly, what I'm observing is the following.</p>
  
  <ul>
  <li>The Activity is rendered.</li>
  <li>The user checks a ListView item.</li>
  <li>The ListView item is displayed in a checked state.</li>
  <li>The <code>onItemClickListener</code> calls <code>getCheckedItemPosition()</code> for the <code>ListView</code> and gets a correct result.</li>
  <li>The user changes the screen orientation.</li>
  <li><code>onCreate()</code> re-builds the 'ListView' and it displays just like before (after <code>onCreate()</code>; see below).</li>
  <li><code>onCreate()</code> calls <code>getCheckedItemPosition()</code> and gets <code>-1</code> despite the <code>ListView</code> showing the correcly checked item</li>
  </ul>
  
  <p>Upon further examination, the following details emerge.</p>
  
  <ul>
  <li><code>onCreate()</code>:
  <ul>
  <li>get <code>ListView</code> resource</li>
  <li>build <code>MyAdapter</code></li>
  <li>set <code>MyAdapter</code> as adapter for <code>ListView</code></li>
  <li><code>getCheckedItemPosition()</code> returns <code>-1</code></li>
  </ul></li>
  <li>after <code>onCreate()</code>:
  <ul>
  <li><code>MyAdapter.getView()</code> is being called</li>
  <li><code>CheckableViewGroup.setChecked()</code> is called with the correct <code>checked</code> value</li>
  <li>the last two steps will be repeated for all items</li>
  </ul></li>
  </ul>
  
  <p>As said before, I'm relying on the Android feature that <code>View</code> objects save their state if they have an ID assigned. I'd say this is the case since there must be some object out there which sets the correct <code>checked</code> status for all the list entries. (By the way, <code>CheckableViewGroup</code> also overrides <code>on{Save,Restore}InstanceState()</code> but that won't be called regardless whether or not it has an ID assigned (presumably because it never gets attached to the layout root?).</p>
  
  <p>So it <strong>looks as if the <code>ListView</code> at the same time knows and does not know its <code>getCheckedItemPosition()</code></strong>? Or am I on the wrong track?</p>
  
  <pre><code>public final class MyActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.my_activity);
          itemList = (ListView)findViewById(R.id.ma_list);
          listAdapter = new MyAdapter();
          itemList.setAdapter(listAdapter);
          itemList.setOnItemClickListener(
              new OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView&lt;?&gt; av, View v, int pos, long id) { checkForm(); }
              }
          );
          listAdapterSetup();
          // this is where itemList.getCheckedItemPosition() returns -1
          // when the listView has been re-built from NonConfiguration data:
          checkForm(); 
      }
  }
  </code></pre>