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:

14536 questions
1084
votes
16 answers

Download a file with Android, and showing the progress in a ProgressDialog

I am trying to write a simple application that gets updated. For this I need a simple function that can download a file and show the current progress in a ProgressDialog. I know how to do the ProgressDialog, but I'm not sure how to display the…
Tom Leese
  • 17,977
  • 11
  • 41
  • 65
693
votes
21 answers

AsyncTask Android example

I was reading about AsyncTask, and I tried the simple program below. But it does not seem to work. How can I make it work? public class AsyncTaskActivity extends Activity { Button btn; /** Called when the activity is first created. */ …
Fox
  • 9,054
  • 13
  • 36
  • 62
686
votes
59 answers

How to check internet access on Android? InetAddress never times out

I got a AsyncTask that is supposed to check the network access to a host name. But the doInBackground() is never timed out. Anyone have a clue? public class HostAvailabilityTask extends AsyncTask { private Main main; …
Vidar Vestnes
  • 41,116
  • 28
  • 81
  • 97
471
votes
7 answers

Android basics: running code in the UI thread

In the viewpoint of running code in the UI thread, is there any difference between: MainActivity.this.runOnUiThread(new Runnable() { public void run() { Log.d("UI thread", "I am the UI thread"); …
Luky
  • 5,017
  • 4
  • 15
  • 14
459
votes
21 answers

How to display Toast in Android?

I have a slider that can be pulled up and then it shows a map. I can move the slider up and down to hide or show the map. When the map is on front, I can handle touch events on that map. Everytime I touch, a AsyncTask is fired up, it downloads some…
Upvote
  • 65,847
  • 122
  • 353
  • 577
390
votes
13 answers

Handler vs AsyncTask vs Thread

I got slightly confused about the differences between Handlers, AsyncTask and Threads in Android. I've read quite a few blogs and questions here in StackOverflow. Handler are background threads that provide you to communicate with the UI. Updating…
Alx
  • 5,995
  • 7
  • 30
  • 51
369
votes
17 answers

How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

I have this two classes. My main Activity and the one that extends the AsyncTask, Now in my main Activity I need to get the result from the OnPostExecute() in the AsyncTask. How can I pass or get the result to my main Activity? Here is the sample…
Stella
  • 3,823
  • 4
  • 13
  • 15
286
votes
3 answers

Warning: This AsyncTask class should be static or leaks might occur

I am getting a warning in my code that states: This AsyncTask class should be static or leaks might occur (anonymous android.os.AsyncTask) The complete warning…
Keyur Nimavat
  • 3,385
  • 3
  • 10
  • 17
264
votes
7 answers

Running multiple AsyncTasks at the same time -- not possible?

I'm trying to run two AsyncTasks at the same time. (Platform is Android 1.5, HTC Hero.) However, only the first gets executed. Here's a simple snippet to describe my problem: public class AndroidJunk extends Activity { class PrinterTask extends…
rodion
  • 5,927
  • 4
  • 20
  • 29
264
votes
12 answers

Is AsyncTask really conceptually flawed or am I just missing something?

I have investigated this problem for months now, came up with different solutions to it, which I am not happy with since they are all massive hacks. I still cannot believe that a class that flawed in design made it into the framework and no-one is…
Matthias
  • 41,630
  • 28
  • 100
  • 129
225
votes
6 answers

Using the "animated circle" in an ImageView while loading stuff

I am currently using in my application a listview that need maybe one second to be displayed. What I currently do is using the @id/android:empty property of the listview to create a "loading" text.
Waza_Be
  • 39,545
  • 47
  • 176
  • 256
196
votes
8 answers

Android. Fragment getActivity() sometimes returns null

In developer console error reports sometimes I see reports with NPE issue. I do not understand what is wrong with my code. On emulator and my device application works good without forcecloses, however some users get NullPointerException in fragment…
162
votes
5 answers

What arguments are passed into AsyncTask?

I don't understand what I am supposed to put in here and where these arguments end up? What exactly should I put, and where exactly will it go? Do I need to include all 3 or can I include 1,2,20?
mergesort
  • 4,615
  • 13
  • 32
  • 58
152
votes
4 answers

android asynctask sending callbacks to ui

I have the following asynctask class which is not inside the activity. In the activity I'm initializing the asynctask, and I want the asynctask to report callbacks back to my activity. Is it possible? Or does the asynctask must be in the same class…
Asaf Nevo
  • 9,962
  • 20
  • 69
  • 144
149
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,097
  • 4
  • 18
  • 12
1
2 3
99 100