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
1126
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
  • 19,309
  • 12
  • 45
  • 70
719
votes
64 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
  • 42,644
  • 28
  • 86
  • 100
711
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,384
  • 13
  • 42
  • 63
499
votes
8 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,346
  • 4
  • 17
  • 15
492
votes
22 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…
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
398
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
  • 6,275
  • 7
  • 32
  • 54
379
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,923
  • 4
  • 15
  • 15
302
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,595
  • 3
  • 13
  • 19
270
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
  • 6,087
  • 4
  • 24
  • 29
265
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…
mxk
  • 43,056
  • 28
  • 105
  • 132
236
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,407
  • 49
  • 186
  • 260
224
votes
19 answers

The AsyncTask API is deprecated in Android 11. What are the alternatives?

Google is deprecating Android AsyncTask API in Android 11 and suggesting to use java.util.concurrent instead. you can check out the commit here * * @deprecated Use the standard java.util.concurrent or *
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
199
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…
165
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
  • 5,087
  • 13
  • 38
  • 63
156
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
  • 11,338
  • 23
  • 79
  • 154
1
2 3
99 100