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
30
votes
7 answers

Android calling AsyncTask right after an another finished

I have some problem with Android AsyncTask. There is an Activity which contains some TextView a button and a picture. When an user entered this activity I start an asynctask to check whether the user can go toward from the activity (until the task…
hcpeter
  • 614
  • 3
  • 11
  • 24
29
votes
4 answers

AsyncTask, must it take such a performance penalty hit...?

I'm developing a small app that reads in specific html-pages, re-formats them and then shows them in a WebView. If I run my code in the GUI thread, the performance hit is close to negligible compared to simply letting the WebView show the original…
OppfinnarJocke
  • 1,038
  • 2
  • 9
  • 21
29
votes
2 answers

How can I make a ProgressDialog be cancelable by the back button but not by a screen tap?

I would like to make a ProgressDialog cancelable by the back button but not by a screen tap. Currently I use setCancelable(true). However, in some newer devices a tap on the screen also cancels the ProgressDialog. I'd like to disable the screen tap…
benkdev
  • 673
  • 2
  • 16
  • 32
29
votes
4 answers

AsyncTask return value

My android app connects to my website to retrieve and upload information so I use an AsyncTask thread. In one instance, I need my thread to return a true or a false value to my main thread. Is there a way to get this return value from an AsyncTask…
sisko
  • 9,604
  • 20
  • 67
  • 139
29
votes
5 answers

How to set up dependency injection using Dagger for things other than Activities and Fragments?

I started setting up dependency injection using Dagger as follows. Please feel encouraged to correct my implementation since I might have mistakes in there! The implementation follows the android-simple example provided by the project. In the…
29
votes
3 answers

Android AsyncTask [Can't create handler inside thread that has not called Looper.prepare()]

I've created an image upload AsyncTask based on a function. And after uploading, I get this error on onPostExecute(). I read up some StackOverflow answers on Runnable yet I kept getting the error over and over again despite implementing a different…
MrYanDao
  • 1,253
  • 1
  • 15
  • 27
29
votes
7 answers

How to completely kill/remove/delete/stop an AsyncTask

I made an app that downloads videos from our server. The issue is: When i cancel the downloading i call: myAsyncTask.cancel(true) I noticed, that myAsyncTask doesn't stops on calling cancel... my ProgressDialog still goes up and its like jumping…
Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
28
votes
2 answers

How to use separate thread to perform http requests

I have an application that is performing HTTP Requests (specifically calling the FogBugz API) when the user clicks certain buttons. Right now, I am just creating a service when the application starts, and then calling different methods in that…
mbauer14
  • 1,217
  • 2
  • 13
  • 18
28
votes
9 answers

Android - AsyncTask not executing

I know there are already a lot of questions with this title but I still haven't found a solution to work for my case. I have the following code that gets the users data from an online database. The problem is, as stated in the title, that the…
Diana
  • 1,417
  • 5
  • 25
  • 48
27
votes
2 answers

How do I use the Eclipse debugger in an AsyncTask when developing for Android?

I'm running ADT (Android Development Tools) in Eclipse and verified that my debugger is working by putting a breakpoint in MainMenu.oncreate (class Activity). But when I put it in the first line of my AsyncTask.doInBackground, it never hits it. I…
Chirag Patel
  • 5,819
  • 8
  • 35
  • 38
27
votes
6 answers

How to raise a toast in AsyncTask, I am prompted to used the Looper

I have tasks completed by AsyncTask in background. At some point I need to issue a Toast that something is completed. I've tried and I failed because Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called…
Pentium10
  • 204,586
  • 122
  • 423
  • 502
27
votes
3 answers

Should I give params to the constructor or to AsyncTask.execute(params)?

I'm trying to figure out why Android's AsyncTask gives parameters through execute and why passing then to the constructor doesn't seem to be done (in the docs, at least). This is the way that seems to me to make the most sense (naturally, the actual…
Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
26
votes
2 answers

Name the Thread of an AsyncTask

Is it possible to give a name to an AsyncTask's background thread, as for normal Threads in Java: Thread(Runnable target, String name) I have seen the code of AsyncTask and the default constructor is just give a name by default private static…
Dayerman
  • 3,973
  • 6
  • 38
  • 54
26
votes
5 answers

Android speech recognizing and audio recording in the same time

My application records audio using MediaRecorder class in AsyncTask and also use Google API transform speech to text - Recognizer Intent - using the code from this question : How can I use speech recognition without the annoying dialog in android…
woyaru
  • 5,544
  • 13
  • 54
  • 92
26
votes
4 answers

Android: How to run asynctask from different class file?

When I use my code in one class file, it runs perfectly: package com.example.downloadfile; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import…
Kris
  • 3,709
  • 15
  • 50
  • 66