Edit: Updated answer to reflect updated question 1:
1) Each time a fetchTodo call is made, a new cursor is created and returned. Keep the previous cursor for the garbage collector. Therefore, if I do not use startManagingCursor or even CursorLoader for this is important, should I call the cursor .close () on the cursor when I am done with this?
Yes, you must definitely say Android on startManagingCursor()
, use LoaderManager
/ CursorLoader
or close()
it yourself. This will not lead to a memory leak, GC will not help with this, since it has its own resources for Cursor
(for example, files are processed in the database).
2) The cursor also has a .deactive () method, and the documentation says it uses less resources (than active cursors). When should I use this? ...
EDIT for other readers: OP found the answer and posted it in his question. The following remains:
I never used deactivate()
(there is no deactive()
), maybe someone can explain this. If you want a truly painless request / update, check out the LoaderManager
structure - this is not only for Honeycomb: using the compatibility library, you can use the LoaderManager
(and Fragments
) up to Android 1.6. This is not only the code for you, but also completely unloads these things on Android, much more than startManagingCursor()
.
EDIT2: some notes on LoaderManager
Developer.android.com has LoaderManager
, but it's pretty ... hard and hard to understand for the first time, like most tutorials. I also had to dig a lot, the best all-in-one stop I've found so far is http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/ (plus all javadocs and compat source lib which you can find) --- The LoaderManager
method LoaderManager
very similar to managed dialogs (now also obsolete, replaced by DialogFragment
) with their onCreateDialog
, onPrepareDialog
, where you simply point Android to "show dialog # 123" and then Android calls your code with this identifier; the same for bootloaders: "load loader # 123", Android calls onCreateLoader()
.
The only obvious flaw initially is that the LoaderManager
relies heavily on the ContentProvider
, and some people really don't like it. Of course, this is additional training and code, but as soon as you have a ContentProvider
for your own data (even if they are used only privately in your application), all data bindings to the view are a breeze with CursorLoader
. IMHO, there is a slight difference between rolling your own "content provider" and actually implementing ContentProvider
- but this is just my opinion very controversial :)
3) I know that startManagingCursor / stopManagingCursor is outdated but I am not targeting Honeycomb (at least for now), and now I donβt want to deal with the new CursorLoader. But in the above, startManagingCursor is used everywhere, but stopManagingCursor is never called once. Why not? Does Android do this in it in its own way? Any situation I should call stopManagingCursor?
Once you call startManagingCursor()
, Cursor
no longer your problem. Android will take care to close the cursor when your Activity
is destroyed (user will move, change orientation, ...). There is no need to map the startManagingCursor()
call to the stopManagingCursor()
call - you usually don't want to take charge of managing Cursor
again once you get rid of it.