Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the Advantages and disadvantages of AsyncTask in Android framework?

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.

like image 521
sofs1 Avatar asked Dec 26 '16 11:12

sofs1


People also ask

What are the disadvantages of AsyncTask in Android?

"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.

What are some limitations of AsyncTask?

The modern AsyncTask is limited to 128 concurrent tasks, with an additional queue of 10 tasks (if supporting Android 1.5, it's a limit of ten tasks at a time, with a maximum queue of 10 tasks). That means that if you queue up more than 138 tasks before they can complete, your app will crash.

What is the use of Async task in Android?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

What is the use of the AsyncTask class?

Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive. Android application runs on a single thread when launched.


1 Answers

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.

like image 135
Rohit Avatar answered Oct 21 '22 01:10

Rohit