0

I have an activity with a spinner which loads a simpleCursorAdaptor. I call another class to return the cursor which is used by the simpleCursorAdaptor. I don't keep a class level variable of the cursor or adaptor.

When this activity closes down I want to close the cursor. Should I:

a) in the activitie's onDestroy() event, get the cursor from the spinner via the adaptor and close it there or

b) In the data handler class which generates the cursor in the first place

WaterBoy
  • 697
  • 3
  • 13
  • 35

3 Answers3

3

If you create the cursor using a loaderManager or a supportedLoaderManager you don't need to worry about closing the cursor. It is also recommended to open a cursor using a loader to do it off the UI thread.

However for some reason if you have to query for the cursor using the contentresolver on the UI thread, it is better to close the cursor in the onPause and requery the cursor in the onResume, worst case scenario close it in the onDestroy, there could be an instance that the activity maybe garbage collected before onDestory is called.

However as mentioned above please try to create the cursor using a CursorLoader with the Loadermanager and LoaderCallbacks

akshaydashrath
  • 1,025
  • 8
  • 18
1

Have you considered the method startManagingCursor(Cursor c)?

http://developer.android.com/reference/android/app/Activity.html#startManagingCursor(android.database.Cursor)

Altough, I think you should use CursorLoader with the LoaderManager (as said in the documentation). This classes are available in the android-support library. But remember, CursorLoader only works with ContentProvider. If you want a CursorLoader with a simple cursor, see this CursorLoader usage without ContentProvider

Edit:

This is now deprecated to Loaders.

Community
  • 1
  • 1
oxygenpt
  • 368
  • 2
  • 17
-1

You have to close the Cursor in onStop() or onDestroy(). Or, you can call startManagingCursor() after you get the Cursor from your query, and Android will close the Cursor on its own.

K_Anas
  • 31,226
  • 9
  • 68
  • 81