Tutorial details

Android: Loaders versus AsyncTask | App Code for Sale | Preview

Android: Loaders versus AsyncTask | Android Tutorial

Loaders and AsyncTask

Overview PAGE TOP

One of the biggest pieces of Android that I have neglected to learn about would be Loaders. Seeing as it’s time for me to learn it, perhaps I can help you out a bit with it as well. My main interest with the Loader concept is how it melds with the tried and true AsyncTask, and if it’s really better or not.


Before getting into the Loader concept, it’s important to have a good idea of what the AsyncTask is and what it’s used for within Android. If you have written any sort of application for Android, chances are you have played with the AsyncTask, or at the very least heard of it. In Android, the AsyncTask class is one of the core development tools that most apps use. It gives the developer an easy way to do processing on a thread that isn’t the UI thread. This keeps the UI thread focused on the UI instead of other time-intensive tasks, such as disk or server calls. There are a few issues with using AsyncTasks, though:

  • Configuration changes can mess things up
  • Pausing an activity doesn’t pause the AsyncTask
  • A fair amount of boilerplate code (which means more possible errors)


The AsyncTask isn’t the only way to do background processing in Android, though. The Loader class is a much newer construct in Android (although now it’s getting a bit dated). It was released with Honeycomb(3.0) and is now included in the Support Library. The beauty of the Loader is that it handles some of the ‘gotchas’ that usually are missed when using the AsyncTask. Mainly, it handles activity configuration changes (IE when the user rotates the screen).

Loaders (specifically the CursorLoader) really shine when using Cursors within Android to pull data. The Loader class does an excellent job of updating the Cursor information (and in turn, the UI) whenever the underlying data changes. This is immensely helpful when information changes often and you don’t want to interrupt the UI, and whatever the user is currently doing, just to display some new information.

One particular subclass of Loaders is of interest: the AsyncTaskLoader. This class performs the same function as the AsyncTask, but a bit better. It can handle Activity configuration changes more easily, and it behaves within the life cycles of Fragments and Activities. The nice thing is that the AsyncTaskLoader can be used in any situation that the AsyncTask is being used. Anytime data needs to be loaded into memory for the Activity/Fragment to handle, The AsyncTaskLoader can do the job better.

To really get into the details of how to actually implement a AsyncTaskLoader, check out these sources:

  • Google has a pretty good example directly in the API Docs.
  • Android Design Patterns provides some more detail and the reasoning behind Loaders.

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.