I'm developing an Android application that is targeting API level 8 (2.2, Froyo). I'm using a ContentProvider and that's simple enough, and I'm using SimpleCursorAdapter to fill out my list view, but I noticed in the documentation for SimpleCursorAdapter that the flagless constructor is deprecated with the following note:
This constructor is deprecated. This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader.
Since I'm targeting API level 8, a LoaderManager isn't tied to an Activity. The FragmentActivity class in the compatibility package does this, but I'm not using Fragments.
My question is: how exactly should I be using LoaderManager/CursorLoader in an app targeting a pre-11 API level? Am I forced to transition to Fragments or should I just revert back to the deprecated SimpleCursorAdapter constructor (but make use of an AsyncTask to keep it UI thread friendly, which is what the CursorLoader is supposed to do)?
Edit:
I've written fairly extensively about the
LoaderManagerin this blog post. Check it out and let me know if its helpful! :)Original post:
Definitely, definitely, definitely go with
LoaderManager. TheCursorLoaderclass offloads the work of loading data on a thread, and keeps the data persistent during short term activity refresh events, such as an orientation change. In addition to performing the initial query, theCursorLoaderregisters aContentObserverwith the dataset you requested and callsforceLoad()on itself when the data set changes, and is thus auto-updating. This is extremely convenient as you don't have to worry about performing queries yourself. Of course it is possible to make use ofAsyncTaskto keep your application UI thread friendly, but it will involve a lot more code... and implementing your class so that it will, for example, retain the loadedCursoroverActivitywon't be simple. The bottom line is thatLoaderManager/Loaderwill do this automatically for you, as well as taking care of correctly creating and closing theCursorbased on theActivitylifecycle.To use
LoaderManager/CursorLoaderin an app targeting a pre-11 API level, simply use theFragmentActivityclass in the compatibility package. AFragmentActivityis just anActivityand has been created for Android compatibility support, and does not require the use ofFragments in your application. Just usegetSupportLoaderManager()instead ofgetLoaderManager()and you should be all set. You could, of course, implement a parentFragmentActivityfor each screen and have it displays a its layout in aFragment(by making use ofFragmentActivity.getSupportFragmentManager()in the Activity'sonCreate()method). This design might make the transition to multi-pane layouts easier if you ever decide to optimize your application for tablets. It's a nice learning experience too :).This is a pretty nice tutorial too. Try and work your way through it and don't hesitate to leave a comment if you have any other questions.