tags:

views:

34

answers:

1

Hi,

I supply a cursor to a ListActivity like this: My question is how/when should I close my cursor when I am done?

Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            mCursorAdapter = new CursorAdapter(this, cursor);
            setListAdapter(mCursorAdapter);
        }

Thank you.

A: 

onDestroy(). Sooner, if you are specifically clearing out the list sooner than that. Or, call startManagingCursor(cursor), and Android will close it for you, when it is safe to do so.

CommonsWare
What should I do in my onDestory() to close the cursor that I passe to CursorAdpater()? Or I have to keep a reference to getContentResolover().query() myself and call 'close()' in the onDestory() manually? Thank you.
michael
@michael: If you are using `getContentResolver().query()`, that `Cursor` is already managed, and so you do not need to close it yourself, as Android will close it for you. I apologize -- I was focused on the cursor and did not realize you had gotten it from a `ContentResolver` instead of SQLite.
CommonsWare