Ok, the documentation states that it lets the Activity manage the cursor's lifecycle. But I don't really see the point of it since when the activity is destroyed, any references to the newly created cursor should be also erased and then the cursor itself is left to perish in the next garbage collecting cycle. So why bother?

- 4,555
- 31
- 31
- 45

- 2,461
- 8
- 36
- 44
-
1This post explains why it is deprecated well :) http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html – user1422551 Aug 17 '12 at 14:14
1 Answers
You should not rely on cursors being destroyed by the garbage collector... a cursor represents a significant amount of resources: all of the data held by the cursor, plus the connection to the content provider that owns the cursor which also means requiring that its process be kept in memory.
In more recent versions of Android, log messages are printed if a cursor's finalizer runs without being explicitly closed, because it is important for apps to close cursors when done with them.
Managed cursors take care of closing the cursor when the activity is destroyed, but they do more than that as well: they will be deactivated and requeried as the activities is stopped and restarted.
That said, at this point you should consider managed cursors to be deprecated. The new Loader API is a lot better, and has many improvements to the user experience of your app -- it ensures that all cursor operations are done off the main thread (so there are not glitches in your UI interactions and animations), and can propagate existing cursor data across activity instances when an activity is restarted due to a configuration change instead of having to reload the data.
If you need to run on older versions of Android than 3.0, you can use the v4 support library's implementation of Loader for those applications.

- 90,665
- 16
- 140
- 154
-
1Ok, that's fair explanation. But could you please elaborate a little more about what you mean when you say that I can use the "v4 support library's implementation of Loader"? I did check out that the CursorLoader class is only available from API level 11. And I just thought that since I want to target lower levels too, there was no way to use that class.. Are you saying I can? – Bilthon Jul 01 '11 at 02:08
-
2@Bilthon: The Android Compatibility Library has `Loader` and `Fragment` support going back to API Level 4. – CommonsWare Jul 01 '11 at 11:02
-
3Does this mean that if the developer handles the closing of Cursors in onPause() and requeries/initialisation of Cursors in onResume(), that startManagingCursor() is unnecessary, and that the mentioned approach can be used in 3.0 + aswell? – ataulm Sep 16 '11 at 10:59
-
6Correct if you are managing the cursor yourself, then you don't want to call startManagingCursor and have the framework also manage it (and fight with you). – hackbod Sep 17 '11 at 07:46