How to transition from managedQuery to LoaderManager/CursorLoader?
Asked Answered
V

1

15

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)?

Veinlet answered 17/1, 2012 at 18:9 Comment(5)
Check out this blog post: Understanding the LoaderManager (part 2)Solfatara
Hello, where is it documented that a LoaderManager is tied to Fragments? It is simply for retrieving data from a DB. The model layer is always detached from the UI layer in any sophosticated software paradigm.Valona
@Valona In the documentation for LoaderManager: Interface associated with an Activity or Fragment for managing one or more Loader instances associated with it. This helps an application manage longer-running operations in conjunction with the Activity or Fragment lifecycle ...and the only types of Activitys that are associated for older platforms are FragmentActivitys.Veinlet
Right, the user said: "Am I forced to transition to Fragments". The answer is NO. A regular Activity will work fine for these purposes.Valona
@Valona Right, but only on the post-fragment APIs. You don't have to transition to Fragments, but you do at least have to transition to an Activity that supports Fragments (which is trivial). Not disagreeing, just making it more clear in case anybody is still supporting GB or earlier.Veinlet
R
23

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 Fragments 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.

Resendez answered 17/1, 2012 at 19:13 Comment(9)
note that there is a typo in the tutorial i posted. other than that, it's great! :)Resendez
Hmmm, I attempted this, but my view was blank. I must have missed something else. But it's nice to have direction! Thanks! One question though, what exactly is the harm in using CursorLoader directly inside my Activity vs. using it inside of LoaderManager? Out of curiosity, would I be able to simply skip using LoaderManager and reap the same benefits (beyond having to manage my own cursor, that is)?Veinlet
There isn't any harm, but you lose all of the benefits of using LoaderManager. For bigger screens it becomes more important that you query on a separate thread since configuration changes involve recreating the entire view layout (and you don't want anything to block this operation on a tablet since layouts are often more complex). By any means you can implement your class to do this on a separate thread using AsyncTask as I mentioned before, but my point is that LoaderManager does this all for you (and manages the cursor too), so its very convenient and its pretty easy to get it working too.Resendez
What specifically are you trying to set up that comes up blank?Resendez
Thank you, that's very informative! To answer your question on the blank view, that was just a lapse of sanity regarding my test data (figures). It works perfectly fine now. This is definitely easier to implement for async queries!Veinlet
The tutorial assumes you have worked through this tutorial, which probably explains your confusion.Resendez
The tutorial is very confusing in Step 3 where it says changes to the onCreate() method. It does not talk about FragmentActivity anywhere. @AlexLockwood I will not say it the best tutorial. It surly need an edit. But +1 for your answer.Suitcase
@AlexLockwood - Hey great suggestions. I followed your answer above and the tutorial you referenced. However, I am getting an IllegalArgumentException. I posted my code and question here. #11804254 If you happen to get some time to look at it and see something wrong, I would appreciate it. Thanks.Sadiron
@Alex Lockwood Is LoaderManager and CursorLoader still best practice these days for, say loading SQLite data into a RecyclerView? Do any of the newer methods like JobIntentService() or JobScheduler offer any major advantages?Defensive

© 2022 - 2024 — McMap. All rights reserved.