Getting familiar with Loaders - Part 1

29 Jan 2017


last modified: 30-jan-2017 (22:33)

This is part 1 of Loaders article series

Why Loaders?

If you do not get something at this point, don’t worry. I will explain each of them with example.

Loaders API


Loader is a class that performs async loading of data. There are a couple of important methods in this class, which you need to get familiar with. Before talking about methods, note this point from docs

Note on threading: Clients of loaders should as a rule perform any calls on to a Loader from the main thread of their process (that is, the thread the Activity callbacks and other things occur on). Subclasses of Loader (such as AsyncTaskLoader) will often perform their work in a separate thread, but when delivering their results this too should be done on the main thread.

Basically, there are 5 important methods in this class. I will explaing them one by one


onStartLoading starts loading of async data in background. It is called automatically by Loader Manager and you are not supposed to call it directly, otherwise you will conflict with loaders internal state.

Behind the scene, Loader manager calls startLoading which updates loader’s internal state and calls onStartLoading. Loader’s state is updated so that isStarted and isReset() will return the correct values.

startLoading method code from android source

  public final void startLoading() {
        mStarted = true;
        mReset = false;
        mAbandoned = false;


onStopLoading is called by Loader Manager when an activity or fragment stops. At this stags, loader can continue loading data in background but it should not deliver updates to the client. It also updates loader’s internal state so that isStarted will return the correct value.

stopLoading method code from android source

 public void stopLoading() {
        mStarted = false;


onForceLoad is invoked by forceLoad when a force load is requested. It is like clearing the cache and requesting new data.


onReset is called automatically by Loader Manager when destroying loader. At this stage, you should free any resource related to this loader since it many never be called again. Before calling this method, loader resets all flags (internal state) so that isStarted and isReset() will return the correct values.

reset method code from android source

    public void reset() {
        mReset = true;
        mStarted = false;
        mAbandoned = false;
        mContentChanged = false;
        mProcessingChange = false;


onCancelLoad is called by Loader Manager when there is a request to cancel the load. It does not cancel the task immidiately, if a task is in progress, it waits for the task to complete and calls OnLoadCanceledListener when task completes.

If the task is already completed or startLoading hasn’t been called, this method will return false.


A loader manager manages the loaders and can be accessed application wide. There are two methods for getting loader manager in an activity/fragment.

LoaderManager getLoaderManager()
LoaderManager getSupportLoaderManager();

Loader manager provides API for initializing, stopping and destroying loaders.

// initialize loader
Loader<D> initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback)
// restart loader
Loader<D> restartLoader(int id,Bundle args, LoaderManager.LoaderCallbacks<D> callback) 
// get loader with ID
Loader<D> getLoader(int id); 
// destroy loader with ID
void destroyLoader(int id); 


From docs

Ensures a loader is initialized and active. If the loader doesn’t already exist, one is created and (if the activity/fragment is currently started) starts the loader. Otherwise the last created loader is re-used. In either case, the given callback is associated with the loader, and will be called as the loader state changes. If at the point of call the caller is in its started state, and the requested loader already exists and has generated its data, then callback onLoadFinished(Loader, D) will be called immediately (inside of this function), so you must be prepared for this to happen.


From docs

Starts a new or restarts an existing Loader in this manager, registers the callbacks to it, and (if the activity/fragment is currently started) starts loading it. If a loader with the same id has previously been started it will automatically be destroyed when the new loader completes its work. The callback will be delivered before the old loader is destroyed.


From docs

Stops and removes the loader with the given ID. If this loader had previously reported data to the client through onLoadFinished(Loader, Object), a call will be made to onLoaderReset(Loader).


Returns the Loader with the given id or null if no matching Loader is found.

LoaderManager Callbacks

A loader manager interacts with client through this callback. It has 3 methods

Instantiate and return a new Loader for the given ID.

Called when a previously created loader has finished its load.

Called when a previously created loader is being reset, and thus making its data unavailable. The application should at this point remove any references it has to the Loader’s data.

Next I will discuss loaders lifecycle in detail. Read it here