Questions tagged [android-asynctask]

Use for questions on android.os.AsyncTask

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most).

WARNING : The AsyncTask has an implicit reference to the enclosing Activity. If a configuration change happens the Activity instance that started the AsyncTask would be destroyed, but not GCd until the AsyncTask finishes. Since Activities are heavy this could lead to memory issues if several AsyncTasks are started. Another issue is that the result of the AsyncTask could be lost, if it's intended to act on the state of the Activity. Replace the AsyncTask by the new AsyncTaskLoader

More information:

14544 questions
155
votes
6 answers

Asynctask vs Thread in android

In UI, to perform some background work, I used a separate Thread. But as suggested by others, I am now using AsyncTask. What is the main difference between a Thread and an AsyncTask? In which scenario, should I use a Thread or an AsyncTask?
Ram
  • 2,532
  • 4
  • 22
  • 25
151
votes
6 answers

Difference between Service, Async Task & Thread?

What is the difference between Service, Async Task & Thread. If i am not wrong all of them are used to do some stuff in background. So, how to decide which to use and when?
SpunkerBaba
  • 2,127
  • 4
  • 18
  • 13
150
votes
18 answers

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

I'm using the support library for my app. In my FragmentActivity I'm using an AsyncTask for downloading data from internet. In the onPreExecute() method I add a Fragment and in the onPostExecute() method I remove it again. When the orientation is…
148
votes
12 answers

AsyncTask and error handling on Android

I'm converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What's unclear to me is how to handle exceptions if something goes haywire in…
Bostone
  • 36,858
  • 39
  • 167
  • 227
130
votes
4 answers

AsyncTaskLoader vs AsyncTask

Since Honeycomb and the v4 Compatibility Library it is possible to use AsyncTaskLoader. From what I understand, the AsyncTaskLoader can survive through config changes like screen flips. Is it recommended to use AsyncTaskLoader instead of AsyncTask?…
OKA
  • 1,453
  • 2
  • 11
  • 7
130
votes
5 answers

Execute AsyncTask several times

In my Activity I use a class which extends from AsyncTask and a parameter which is an instance of that AsyncTask. When I call mInstanceOfAT.execute("") everything is fine. But the app crash when I press an update button which calls again the…
Dayerman
  • 3,973
  • 6
  • 38
  • 54
128
votes
13 answers

java.net.UnknownHostException: Unable to resolve host "": No address associated with hostname and End of input at character 0 of

I've created an app that loads a question from my web services, and it works fine. But, sometimes it crashes and I do not get the reason why this is happening, especially because I have also given it the required permissions. It works fine, but at…
Reshma
  • 1,449
  • 2
  • 10
  • 17
127
votes
7 answers

Android - Setting a Timeout for an AsyncTask?

I have an AsyncTask class that I execute that downloads a big list of data from a website. In the case that the end user has a very slow or spotty data connection at the time of use, I'd like to make the AsyncTask timeout after a period of time. My…
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
126
votes
9 answers

Return a value from AsyncTask in Android

One simple question: is it possible to return a value in AsyncTask? //AsyncTask is a member class private class MyTask extends AsyncTask{ protected Void doInBackground(Void... params) { //do stuff return…
tom91136
  • 8,662
  • 12
  • 58
  • 74
118
votes
4 answers

Android: How can I pass parameters to AsyncTask's onPreExecute()?

I use an AsyncTask for loading operations that I implemented as an inner class. In onPreExecute() I show a loading dialog which I then hide again in onPostExecute(). But for some of the loading operations I know in advance that they will finish…
Steven Meliopoulos
  • 4,450
  • 4
  • 34
  • 36
112
votes
4 answers

AsyncTask threads never die

I'm using AsyncTasks to fetch data in response to the user pressing a button. This works well and keeps the interface responsive while fetching the data, but when I checked out what was going on in the Eclipse debugger, I found out that every time a…
Computerish
  • 9,590
  • 7
  • 38
  • 49
109
votes
9 answers

Ideal way to cancel an executing AsyncTask

I am running remote audio-file-fetching and audio file playback operations in a background thread using AsyncTask. A Cancellable progress bar is shown for the time the fetch operation runs. I want to cancel/abort the AsyncTask run when the user…
Samuh
  • 36,316
  • 26
  • 109
  • 116
104
votes
11 answers

Unable to resolve host "" No address associated with hostname

I tried following this tutorial: Getting Data from the Web I tried implementing it on Android 3.0, the latest platform for tablets, however, I get this error: Unable to resolve host "www.anddev.org" No address associated with hostname. You can…
95
votes
3 answers

Android AsyncTask threads limits?

I am developing an application where I need to update some info every time user logs in to the system, I also use database in the phone. For all those operations (updates, retrieving data from db and etc.) I use async tasks. As up till now I didn't…
Mindaugas Svirskas
  • 1,039
  • 1
  • 9
  • 13
92
votes
4 answers

Android AsyncTask for long running operations

Quoting the documentation for AsyncTask found here, it says: AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the…
user1730789
  • 5,157
  • 8
  • 36
  • 57