How to switch from managedQuery to LoaderManager / CursorLoader?

I am developing an Android application that targets API level 8 (2.2, Froyo). I use ContentProvider and it is simple enough and I use SimpleCursorAdapter to populate my list, but I noticed in the documentation for SimpleCursorAdapter that the flagship constructor is deprecated with the following note:

This constructor is deprecated. This parameter is not recommended, as it leads to the execution of cursor requests in the application user interface thread and, therefore, can lead to poor responsiveness or even to application errors that do not respond. Alternatively, use LoaderManager with CursorLoader.

Since I am targeting API level 8, LoaderManager not tied to Activity . The FragmentActivity class in the compatibility pack does this, but I don't use fragments.

My question is: how exactly should I use LoaderManager/CursorLoader in an application targeted at API level up to 11? Am I forced to go to fragments or do I need to go back to the legacy SimpleCursorAdapter constructor (but use AsyncTask to support a stream of user interfaces, which CursorLoader should do)?

+15
java android android-fragments simplecursoradapter android-loadermanager
Jan 17 '12 at 18:09
source share
1 answer

Edit:

I wrote quite a LoaderManager about LoaderManager in this blog post . Check it out and let me know if this is helpful! :)




Original post:

Definitely, definitely, with the idea of LoaderManager . The CursorLoader class offloads the work of loading data in a stream and keeps the data constant for short activity refresh events, such as orientation changes. Besides executing the initial request, CursorLoader registers a ContentObserver with the ContentObserver you requested and calls forceLoad() on its own when the dataset is changed and thus automatically updated. This is very convenient, since you do not need to worry about executing queries yourself. Of course, you can use AsyncTask to support the user-friendly interface of the application, but it will include much more code ... and implementing your class, for example, to keep the loaded Cursor Activity , for example, will not be simple. The bottom line is that LoaderManager/Loader will do this automatically for you, and also takes care of creating and closing Cursor correctly based on the Activity life cycle.

To use the LoaderManager/CursorLoader in an API level 11 application, simply use the FragmentActivity class in the compatibility pack. A FragmentActivity is just an Activity and was created to support Android compatibility and does not require the use of Fragment in your application. Just use getSupportLoaderManager() instead of getLoaderManager() and you have to be configured. Of course, you could implement the parent FragmentActivity for each screen and display its layout in Fragment (using FragmentActivity.getSupportFragmentManager() in the Activity onCreate() method). This design can facilitate the transition to layered layouts if you ever decide to optimize your tablet app. This is a good learning experience :).

This is a pretty nice tutorial . Try and work your way through it and feel free to leave a comment if you have other questions.

+23
Jan 17 '12 at 19:13
source share



All Articles