8

I am learning Android app development from Udacity.com by Google engineers and they said,

"It is not a good idea to use 'AsyncTask' as it is not attached to an activity life cycle. The virtual machine will hold on to the activity object as long as the Asynctask is running, even after Android has called onDestroy( ) method for the activity and expect it to be discarded.

If you rotate your phone, the behavior is to destroy the current activity and instantiate a new one. The naive AsyncTask implementation now has two threads trying to do the same update. So it is not the best pattern for a potentially very long running background operation , such as fetching from web services. If you leave the app, the asyncTask will run as long as as the process is kept alive , but will run at a lower priority, and your process will be the first thing to be killed if the device needs more resources. "

1) If using AsyncTask is disadvantageous why was it created? What would have been the design philosophy or the cause to create it in spite of having services(or something similar to achieve same kind of functionality)?

2) What are the situations where Asynctask should be used for betterment compared to Services/similar options available in Android?

3) What are the situations/places Asynctask should never be used?

Please do not downvote this question. I searched Stackoverflow and I couldn't find a similar question.

2
  • 1
    This is an excellent question. Did you find an answer? I am wondering the same Commented Sep 6, 2018 at 15:13
  • @IgorGanapolsky I don't remember. I paused that android project.
    – sofs1
    Commented Sep 7, 2018 at 22:54

1 Answer 1

8

Advantages of AsyncTask

  1. Provides generic solution for all network calls
  2. Publish progress to UI while executing.
  3. Run Asynchronously
  4. Easy to maintain and read.

Problems in AysncTask

  1. When you rotate your screen, Activity gets destroyed, so AsyncTask will not have a valid reference to publish data from onPostExecute(). In order to retain it, you need to usesetRetainState(true) if calling from fragment or onConfigChanges() if calling from activity method of an activity.
  2. If activity gets finished, AsyncTask execution will not cancelled automatically, you need to cancel them else they will keep on running in the background.
  3. If any exception occurs while performing network task, you need to handle them manually.

Whereas AsycTask, Services, IntentService, Threads all run on different threads and all serve different purpose. please read more detail here.

So you need to decide when to use which component while performing non UI operations.

2
  • "... When you rotate your screen, Activity gets destroyed, so AsyncTask will not have a valid reference to publish data from onPostExecute() ...". This is not a problem of AsyncTask. The problem is with usage you suggest. You should not reference any UI component (e.g. Activity) from AsyncTask. You should just publish "async task finished event" in your onPostExecute and any consumer (an Activity for example) may or may not react to this "async task finished event". Commented Sep 5, 2018 at 17:36
  • What do you mean by ~"easy to maintain"? Commented Sep 6, 2018 at 15:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.