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.
Apart from the obvoius features like ease or use and being less error prone , the most interesting features of loaders are
- They monitor the source of their data and deliver new results when the content changes.
- They automatically reconnect to the last loader’s cursor when being recreated after a configuration change avoiding the need to re-query their data.
- 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
- 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.
- 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.
- This is the base class of any loader that asynchronously loads data. All loaders
- Abstract loader that provides an AsyncTask to do the work. One can extend this to implement a custom loader.
- 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.
This is a complete example of using a CursorLoader to load contacts into a fragment that displays a data on the list view.
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.