justanoob justanoob - 3 months ago 26
Android Question

Can AsyncTaskLoader replace AsyncTask?

I feel like

is basically a better
, although i'm not entirely sure i should replace my
implementations with

What confuses me is the
name kinda suggests it should be used for loading data.

What if i'd like to write data (into a
for example) or do any other background operation (that is not loading)?

s suitable for any background operation, that i would normally use an


Loaders are intended for loading data, yes. The hole that they fill is that you can "re-connect" to it on configuration change when you call LoaderManager#init(). What this will do is,

  1. If the Loader has not started, it will start the Loader.
  2. If the Loader has started but not finished, it will connect the callback to the Loader and give the callback the data when it's loaded.
  3. If the Loader has started and finished, it will not start the Loader and pass the information loaded to the callback.

So that means if the user had the phone in a pocket and it just kept rotating over and over again, you wouldn't start a hundred Loading tasks to get one single piece of data.

You wouldn't really care for single, outbound tasks that are fire-and-forget. You may also not want this to happen as you'll probably want all file-writes to execute which would not be the case with Loaders unless you restarted them every time. In which case, you're defeating the purpose of Loaders.