Search code examples
androidandroid-loader

What's defference between AsyncTaskLoader and Loader?


Cannot understand when I need to use Loader and when AsyncTaskLoader? I read docs by these class but I understood it badly. As I understood Loader allows to do a background work so it must be launched in non-GUI thread, it isn't? Why does AsyncTaskLoader do all same? I'm confused already.
Please, someone explain me more details.


Solution

  • All of the Loader methods are called on the main thread - Loader does not do any work on a background thread. This allows you to use whatever loading mechanism you want - be it a separate thread, callbacks to another component, or anything else.

    AsyncTaskLoader extends Loader and adds loadInBackground(), a method specifically called on a background thread. It is simply a convenience class for the simple case.

    In either case, the real role of Loaders is to make loading data lifecycle aware as explained in this blog post - Loaders (and hence, AsyncTaskLoaders) survive configuration changes such as screen rotations. The blog contains a number of examples of AsyncTaskLoader, using an AsyncTaskLoader with an observer of changes, and a Loader which gets data from another data source using callbacks.