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 LoaderManager
in this blog post. Check it out and let me know if its helpful! :)
Original post:
Definitely, definitely, definitely go with LoaderManager
. The CursorLoader
class 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, the CursorLoader
registers a ContentObserver
with the dataset you requested and calls forceLoad()
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 of AsyncTask
to 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 loaded Cursor
over Activity
won't be simple. The bottom line is that LoaderManager/Loader
will do this automatically for you, as well as taking care of correctly creating and closing the Cursor
based on the Activity
lifecycle.
To use LoaderManager/CursorLoader
in an app targeting a pre-11 API level, simply use the FragmentActivity
class in the compatibility package. A FragmentActivity
is just an Activity
and has been created for Android compatibility support, and does not require the use of Fragment
s in your application. Just use getSupportLoaderManager()
instead of getLoaderManager()
and you should be all set. You could, of course, implement a parent FragmentActivity
for each screen and have it displays a its layout in a Fragment
(by making use of FragmentActivity.getSupportFragmentManager()
in the Activity's onCreate()
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.
来源:https://stackoverflow.com/questions/8899497/how-to-transition-from-managedquery-to-loadermanager-cursorloader