Android SQlite Leak Problem with CursorAdapter
<p>I have a <code>ListView</code> that displays data from an sqlite database. I extended <code>CursorAdapter</code> to do this. The following code in my <code>ListActivity</code> has one big problem: it hands off the cursor to the <code>CursorAdapter</code></p>

<pre><code>private boolean refreshListView() {
    final boolean result;
    SqliteHelper sqliteHelper = new SqliteHelper(this);
    sqliteHelper.open();
    final Cursor cursor = sqliteHelper.fetchAll();
    if (cursor.getCount() == 0) {
       //handling some stuff
    } else {
        startManagingCursor(cursor);
        final MyCursorAdapter measures = new MyCursorAdapter(this, cursor);
        setListAdapter(measures);
        result = true;
    }        
    return result;
}
</code></pre>

<p>I noticed that this code leads to frequent <code>IllegalStateExceptions</code> "SQLiteDatabase created and never closed". </p>

<p>I tried to fix this by closing the database connection. But if I close it from this method I get <code>SQLiteMisuseException</code> which I am guessing is caused by the Adapter still being at work and the cursor is still needed? </p>

<p>I also tried closing the database from <code>onPause()</code>. This does not fix the leakage however. </p>

<p>I don't want an app that produces any kind of memory or other leak. But I have no idea what I am doing wrong. I have not found any call back or lifecycle methods in either <code>ListActivity</code> or <code>CursorAdapter</code> that gave me a hint how I should be handling this. </p>

<p>I'd be grateful for any hint on how to fix this. I am starting to suspect that the entire construct is just wrong. I can post more code if needed. </p>
