Android: Loaders versus AsyncTask

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.
 
 
 
 
 
 
 

AsyncTask

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)

Loaders

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:

 

Reference: Android: Loaders versus AsyncTask from our JCG partner Isaac Taylor at the Programming Mobile blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

4 Responses to "Android: Loaders versus AsyncTask"

  1. Hi, I think you need to correct the statement. “The AsyncTask isn’t
    the only way to do background processing in Android,” Please refer
    android developer blog for better understanding of background processing Android.

    • Sameer says:

      Mr. Sree
      “The AsyncTask isn’t the only way to do background processing in Android.” is damn correct. As you can perform background Task using Thread and Handler or Service also. I have Gone through android since 2 years.

  2. Sameer says:

    Yes correctly pointed out issue. Handling asynchronous Task while changing orientation is quite mess the things. But loader itself can not perform the background task, It uses asynchronous and then just stop calling same process again by saving result automatically while activity recreating. It delivered the same result to activity again. Using Loader Manager To handling orientation in better way

  3. Anonymous says:

    To fetch data from webservices, Shall we use AsyncTaskLoader or just AsyncTask ?

Leave a Reply


× 8 = sixty four



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close