[TUT] ASyncTaskLoader using Support Library

I’ve just started learning about Loaders, they’re a way to handle doing tasks off the UI thread. The benefits include having a listener that will inform your fragment/activity when the data your monitoring is changed. They will also account for orientation changes when loading.

asynctaskloader using support library example

A brief introduction is written here and it got me up to speed: An Intro to Loaders

This tutorial has extensive code but is pretty simple in concept. It follows on from the tutorial already written at https://developer.android.com/reference/android/content/AsyncTaskLoader.html. The Google tutorial shows you how to use an ASyncTaskLoader but with the post HoneyComb API. So I’ve taken this code and used it but coded against the support library.

There are two changes and one bug fix that need to be implemented to get this to work! I’ve also increased the OO concepts using correct packaging and class files.

  • Changed out the HC ArrayAdapter and used BaseAdapter
  • Removed the use of the ActionBar search function
  • ASyncTask wont start unless you explicity call forceLoad()!

First off we can’t use the helper methods in extending HC ArrayAdapter, we extend BaseAdapter and do the data management ourselves. In the long run this is probably a better approach for control and customisation.

AppListAdapter.java

I removed the ActionBar search functionality, I don’t think it should be part of this tutorial and just stops you seeing the wood for the trees as they say. So here’s the new fragment.

AppListFragment.java

Finally there is a bug when extending ASyncTaskLoader from the support library, the loader doesn’t actually start. It’s written up as an issue here. What you need to do is catch when the loader wants to start loading using onStartLoading(), then query if you have your data yet (because the loader could be restarted) if you have then deliver your data, else force it to start.

That should do it, if you run up your app you’ll see a list of all the applications running on your device. I also changed the fragment to be instantiated in XML instead of with the fragment manager. This was only done in the Google example because it’s part of a bigger package of examples.

activity_main.xml

For the full source code see below:

Download link for the eclipse project source

GitHub Repo Mirror


4 thoughts on “[TUT] ASyncTaskLoader using Support Library

  1. Loaders is really helpful for our website making, its make us aware about our updates, we can resets and more options are available. Thank You for the informative article.

  2. Pretty good tutorial. There’s something that’s bugging me, perhaps more than it should and that is why do we do this:

    @Override
    public void deliverResult(List apps) {
    if(isReset()){
    // An async query came in while the loader is stopped. We don’t need the result
    if(apps != null){
    onReleaseResources(apps);
    }
    }

    }

    Why would the Loader be in a reset status? 😕 I mean, it IS deliveringResults, right? as the method suggests. My question is, when does a Loader enters the reset status, why, and why is it that we don’t show data when we’re in it.

    1. Reset means stopped, so imagine you’re activity asks to load some list but whilst it’s hitting the database the user navigates elsewhere, the loader is no longer needed and therefore the result is not needed either

Comments are closed.