I use the same cursor multiple times in the custom view in its different methods. Should I close the cursor after each use or can I keep it open until the view is destroyed? And the same for the database, can it be opened once the activity that holds this view is created and closed when the activity is destroyed? I continuously receive an error ‘close() was never explicitly called on database…’ when I do as described above.
Share
Depends. If the cursor result is not going to change meanwhile (e.g. remote connection modifying the DB), then it’s OK. Don’t forget to use
CursorLoaderto have Android manage your cursor automatically (e.g. close the cursor if your app crashes).Yes, you can open in
onResume()and close inonPause()callbacks, or when you know your database is not going to be queried anymore.This is normal only if your app crashes. Should not happen if you’re doing it like I said above.