Realistically, you probably want to use a networking library like Volley. This has some nice features like request batching and image caching. Nonetheless, for the sake of argument lets compare Service
, Loader
s and AsyncTask
.
Services are the way to go if you want to allow the loading to continue while changing Activities or backgrounding your application. Or, if you want to export your service so multiple applications can use it. Otherwise, use a Loader or AsyncTaskLoader.
Loaders have a few advantages over AsyncTasks.
- They are less likely to cause crashes by executing code after the Activity has finished, since they are aware of the android lifecycle.
- The design discourages having references to
View
s or Activities. This reduces the likelihood of forcing the Activity to stay in memory after it has already finished.
- Monitor the data source for changes and trigger callbacks when they occur
- They have built in caching that can be useful after rotations. For
Cursor
s, the CursorLoader
automatically reconnects at the correct position to the last Cursor loaded
However, they also have disadvantages
- The API is extremely more cumbersome than
AsyncTask
. Especially if you care about compatibility with older versions of Android
- You are already storing UI state inside onSaveInstanceState(), so using the
Loader
's causes you to save state in multiple ways. This can be confusing to read and understand. Especially if you end up mixing retained fragments into the mix.
- The
Loader
caches the loaded result, not the UI state that you actually need
I'm assuming you are just reading from web services, not writing. If you are performing updates to a web service and you need to see the service's response, then this changes things. Using an AsyncTask
could prevent you from getting the response if the it is received during a rotation.