last modified: 30-jan-2017 (22:33)
This is part 1 of Loaders article series
If you do not get something at this point, don’t worry. I will explain each of them with example.
An abstract class that performs asynchronous loading of data.
Manages async loading of data.
Callbacks for handling data.
Implementation of loader which uses async task to load data in background.
Sub class of AsynTask loader. It handles quering on background thread.
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
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
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
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.
Loader manager provides API for initializing, stopping and destroying loaders.
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.
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.
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.
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