views:

25

answers:

1

In an Android app I developed, I open a cursor to a query with a fairly large result set (~1k rows) and I keep it open indefinitely. Is this an ok practice? If not, should I close the cursor in the onClose() handler and then re-open it in the onStart() handler?

The app seems to run fine, however it sometimes randomly crashes after having been idle for a long period of time and I think it might be related to the cursor.

A: 

The cursor should not be left open forever because it will leave the database in an improper state when your app is killed eventually, causing some errors to show in the Log if you've noticed.

What you can do is close it in onStop and re open it in onStart. Alternatively you can let Android handle this automatically for you by calling startManagingCursor(c) from within your activity.

docs

smith324
I close it in the onDestroy method. I actually found the root cause of the issue and it's not related to the cursor afaik. I want to keep the cursor open because it is expensive to recreate it.
Gordon
As long as its closed at some point you should be fine. Just be aware that onDestroy may not be called all the time. (check out the lifecycle)
smith324