0

Do I need to be concerned about making sure the database is closed when a FragmentActivity that uses a LoaderEx SQLiteCursorLoader is paused or destroyed?
Thanks much

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Julian A.
  • 10,928
  • 16
  • 67
  • 107

1 Answers1

1

When the FragmentActivity is destroyed, it should call reset() on all Loader instances it created, which will call onReset() in the Loader implementation, which in SQLiteCursorLoader calls close() on any outstanding Cursor.

Note that this happens when the activity is destroyed. There does not seem to be a Loader equivalent of the managed Cursor concept of deactivating the Cursor when the activity is paused. That's a pity, as that was a nice heap management feature, and I'll ponder how to best support that.

If you find evidence that something is being leaked, file an issue in the LoaderEx project with details of how to reproduce the error.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • +1, but if I understood correctly, one of the claimed advantages of `Loader` is that data (`Cursor` in this case) is retained after the `Activity` is destroyed - for example on configuration change. This is possible because the `LoaderManager` life is decoupled from the `Activity` one and may span over (please, correct me if I'm wrong). But this no longer stands if we close everything `onDestroy()`. Why not using a plain `AsyncTask` then? How can one keep track of all of the created `Loader`s? And what about closing the `SQLiteOpenHelper`? That's important, too – Raffaele Oct 11 '12 at 00:34
  • @Raffaele: `FragmentActivity` (and the baseline `Activity` of API Level 11+), have the smarts to deal with configuration changes properly. In this answer, when I wrote "is destroyed", I really meant "is destroyed for the final time, not due to a configuration change". I apologize for any confusion. – CommonsWare Oct 11 '12 at 09:55
  • And what's the callback associated with this *real destruction*? One needs it to close loaders. One also needs to ensure that when this happens there's no `Loader` running. Moreover, the `LoaderManager` API doesn't list the init'ed `Loader`'s, so one must keep track of them by hand, or just init and closing every possible loader, but this approach is feasible only for *fixed* loaders, and can't support *loaders on demand* – Raffaele Oct 11 '12 at 10:42
  • @Raffaele: "One needs it to close loaders" -- `FragmentActivity` "closes" loaders (via a call to `reset()`), as I mentioned in the opening paragraph of the answer to which you are commenting. "One also needs to ensure that when this happens there's no Loader running" -- or, just let the `AsyncTask` inside the loader run to completion, since that will only take a moment anyway in man cases. If you have further questions regarding the `Loader` framework, please read the source code -- the Android Support package's code may already be on your hard drive. – CommonsWare Oct 11 '12 at 10:49