I have a main activity which user a viewpager and a database adapter to serve up the views.
Initially all the rows from the Db are pulled into the cursor (Async)
Once this cursor is returned the user can add various search criteria from different activities which then runs all the data through an Async scoring algorithm.
I was getting some Database errors about having not closed the database so I have closed the cursor and the database in onPause and re-instantiated them in onResume. This would work fine but the activities which build up the search criteria for scoring are started for result. When they return to the main thread onActivityResult is called before on resume so I can’t run the scoring algorithm from here as the cursor doesn’t exist yet (it has to be recreated in onResume).
now, If I want to be able to switch activities while the asyncTask is running I can’t close the cursor and Db in onPause.
Does anyone have any ideas? Is my “design” just fundamentally flawed?
thanks, m
You should be passing the cursor to the activity to manage, so that when the activity is removed the cursor is automatically closed.
Hmm, although when I looked up the reference it suggested a new cusorLoader and using the compatibility libraries.
This link should get you started though
Developer Docs – Activity.startManagingCursor()