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.
"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.
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.
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 .
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.
Advantages of AsyncTask
Problems in AysncTask
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.AsyncTask
execution will not cancelled automatically, you need to cancel them else they will keep on running in the background.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With