Methods for loading data into Async for Android

If I need to download some data asynchronously via HTTP (or something else) to update the user interface, I have several options when writing an Android application (among many others that I'm sure I skipped):

  • Use a regular thread and handler to update the user interface.

  • AsyncTask

  • Use an IntentService and use either a callback or pass the results through Intent.

  • Using Loaders .

From what I understand, IntentService is not tied to the Activity lifecycle, so any orientation changes, etc. will not affect data retrieval. If this does not apply to AsyncTask or a thread running inside an Activity.

The reason for the question is that I recently read about Loaders and are confused about their application. They seem to be more closely related to the data source, where if the data source changes, then everything is “transparently” processed accordingly. Loaders also seem tolerant of configuration / orientation changes (I believe).

I am currently using IntentService to make RESTful service calls and translate the results that will be received by the corresponding actions.

I suppose I can write an HTTP downloader, but I'm not sure if this is the best way to use this mechanism.

What are the advantages / disadvantages of using one of the async data loading methods for any other?

+8
android
source share
1 answer

All of these mechanisms are just options. There is no such thing as one size fits the entire tool, and therefore all of these different methods for performing the same task is a way to cover as many use cases as possible.

Ultimately, you decide which method makes more sense for your scenario. But for a kind of general explanation of what you should use ...

  • Regular thread and handler - Why, when are there other, simpler options?

  • AsyncTask. Since AsyncTask will almost always depend on Activity, use this when you need to load data asynchronously and you are 100% sure how long it will take. Example: Executing an SQLite query.

  • IntentService / Service - Services are not activity-related, such as AsyncTask, so they are ideal for scenarios in which you may not know how long it will take to complete. Example: loading data from a web API and updating a database.

  • Loaders - loaders are designed to simplify the process of loading data and filling it in the user interface. The nature of the Loaders sorter implies that the data that you upload will be presented to the user in a list. Example: loading data and filling it in ListView

+2
source share

All Articles