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
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