Keeping Open a Sqlite Cursor
In an Android app I developed, I open a cursor to a query with a fairly large result set (~1k rows) a开发者_JAVA技巧nd 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.
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
精彩评论