JF0001
JF0001

Reputation: 839

Closing a getContentResolver Cursor

I am populating a Cursor by calling getContentResolver as follows in my onCreate method:

Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, "oid, name, flag");

The Cursor is used when creating an instance of a SimpleCursorAdapter.

On the query keyword (in line of code above), I am receiving the following warning:

The cursor should be freed up after use with #close

I have tried to close this Cursor at the very end of my onCreate method, after instantiating my SimpleCursorAdapter class, but this makes the application crash.

Where and when should I close the Cursor?

Upvotes: 1

Views: 478

Answers (1)

Dor Solomon
Dor Solomon

Reputation: 91

Because you're using the Cursor for your SimpleCursorAdapter, closing it after calling the setAdapter() would prevent the adapter from accessing the data. Therefore you need to find another place to close it, perhaps in the activity tear down life cycle stages, such as onPause(), onStop() or onDestory(). (My suggestion is to close the Cursor in the onStop() becuase it's guaranteed to get called).

Upvotes: 1

Related Questions