Skip to content
September 23, 2011 / amsanjeev

An Introduction to Loaders in Android


Loaders is a concept introduced in Android starting from the HoneyComb build. It gives the developer a mechanism of loading data asynchronously for an activity or fragment. Since loaders are specifically designed to solve the issue of async loading, one does not have to spend too much time designing async tasks to handle all different scenarios efficiently.

With the availability of additional screen real estate the amount of data that applications need to fetch to display it on the screen has increased and the need to do this more efficiently without affecting the user experience is absolutely important .

Need for Loaders

For lengthy operations like loading data from a data source working on the UI thread is a bad option. More over if the data is being loaded over the network then the latency is something that cannot be predicted. The common design pattern used for above problem is to create an Async Task and hand over the data loading task to it. Loader provides the exact same feature with a well-defined and simple API.

Loader also persist the data fetched to avoid repetitive fetch operations for simple activity refresh event like orientation change, keyboard open etc. Internally loaders also use async tasks to perform the data load. So there is no performance gain that one would notice when compared to async tasks, provide that the async tasks are designed and developed properly.

The bottom line is that it even though it is possible to implement the same using async tasks it is far more complicated and time consuming to do that . And also its more error prone and hence requires a lot of testing time . With loaders it has become lot more simpler and faster to implement data loading and manage the lifecycle events.

Interesting features

Apart from the obvoius features like ease or use and  being less error prone , the most interesting features of loaders are

  1. They monitor the source of their data and deliver new results when the content changes.
  2. They automatically reconnect to the last loader’s cursor when being recreated after a configuration change avoiding the need to re-query their data.
  3. They are available as a part of the compatibility library. So developers can use it in applications that run on android build previous to HoneyComb.


Different components involved

  • LoaderManager
    • This abstract class is used to manage the different loaders that one would be using in an activity or fragment and tie the loader operations to the activity/fragment lifecycle.
  • LoaderManager.LoaderCallBacks
    • This is a nested class that provides a callback interface to LoaderManager so that the client can interact with the load manager .
    • The following functions have to be implemented by the user.
      • onCreateLoader() : Instantiate and return a new Loader for the given ID.
      • onLoadFinished() : Called when a previously created loader has finished its load.
      • onLoaderReset() : Called when a previously created loader is being reset, thus making its data unavailable.
  • Loader
    • This is the base class of  any loader that asynchronously loads data. All loaders
  • AsyncTaskLoader
    • Abstract loader that provides an AsyncTask to do the work. One can extend this to implement a custom loader.
  • CursorLoader
    • This is an convenience class provided that could be used to query content providers and return cursor to work with. It is a subclass of AsycnTaskLoader.

Code Snippets

This is a complete example of using a CursorLoader to load contacts into a fragment that displays a data on the list view.

Conclusion

I hope this article helps you understand the concept of Loaders in Android a bit better and encourage you to use Loaders in your applications.


Advertisements

5 Comments

Leave a Comment
  1. news / Jun 7 2012 4:26 pm

    A insightful post right there mate . Cheers for that .

  2. Piyush Patel (@piyushnp) / Jun 29 2012 11:41 am

    Nice article.

  3. Naresh / Sep 14 2012 5:37 pm

    simple and interesting article. Thank you.

  4. jankanar / Feb 5 2013 5:12 am

    Howdy I am so excited I found your blog, I really found you by error, while I was searching on Digg for something else, Anyhow I am here now and would just like to say thanks a lot for a marvelous post and a all round exciting blog (I also love the theme/design), I don’t have time to read through it all at the minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read more, Please do keep up the great job.

Trackbacks

  1. [TUT] ASyncTask Loader using Support Library | Blundell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: