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
90
votes
9 answers

Android SDK AsyncTask doInBackground not running (subclass)

As of 15/2/2012 I have yet to find a good explanation to nor a reason why this does not work. The closest to a solution is to use the traditional Thread approach, but then why include a class that does not (seem to) work in the Android SDK? Evenin'…
SeruK
  • 987
  • 1
  • 8
  • 14
88
votes
11 answers

Android check null or empty string in Android

In my trying AsyncTask I get email address from my server. In onPostExecute() I have to check is email address empty or null. I used following code to check it: if (userEmail != null && !userEmail.isEmpty()) { Toast.makeText(getActivity(),…
user3384985
  • 2,975
  • 6
  • 26
  • 42
88
votes
13 answers

How to handle an AsyncTask during Screen Rotation?

I read a lot on how to save my instance state or how to deal with my activity getting destroyed during screen rotation. There seem to be a lot of possibilities but I haven't figured out which one works best for retrieving results of an AsyncTask. I…
Janusz
  • 187,060
  • 113
  • 301
  • 369
86
votes
12 answers

Android Fragments. Retaining an AsyncTask during screen rotation or configuration change

I'm working on a Smartphone / Tablet app, using only one APK, and loading resources as is needed depending on screen size, the best design choice seemed to be using Fragments via the ACL. This app has been working fine until now being only activity…
86
votes
4 answers

getting context in AsyncTask

I am trying to get the context in my AsyncTask of the class called Opciones(this class is the only one that call that task) but I don't know how to do it, I saw some code like this: protected void onPostExecute(Long result) { …
D4rWiNS
  • 2,585
  • 5
  • 32
  • 54
84
votes
6 answers

How can you pass multiple primitive parameters to AsyncTask?

There are related questions, such as How can I pass in 2 parameters to a AsyncTask class? , but I ran into the difficulty of trying in vain to pass multiple primitives as parameters to an AsyncTask, so I want to share what I discovered. This…
robguinness
  • 16,266
  • 14
  • 55
  • 65
77
votes
6 answers

AsyncTask won't stop even when the Activity has destroyed

I have an AsyncTask object which starts executing when the Activity is created and does stuff in the background (downloads up to 100 images). Everything works fine but there is this peculiar behavior which i'm not able to understand. For eg: when…
Raja
  • 6,354
  • 9
  • 30
  • 34
70
votes
6 answers

iOS/Objective-C equivalent of Android's AsyncTask

I'm familiar with using AsyncTask in Android: create a subclass, call execute on an instance of the subclass and onPostExecute is called on the UI thread or main thread. What's the equivalent in iOS?
SundayMonday
  • 19,147
  • 29
  • 100
  • 154
65
votes
7 answers

ProgressDialog in AsyncTask

I'm trying to display a custom progressdialog while loading RSS feed from an HTTP server, I made a hard search, but nothing helped me to do this, the only thing I know is that the solution should use AsyncTask, but I'm confusing about the params to…
Houssem
  • 2,069
  • 3
  • 27
  • 42
64
votes
6 answers

Android - Cancel AsyncTask Forcefully

I have implemented AsyncTask in my one of activity: performBackgroundTask asyncTask = new performBackgroundTask(); asyncTask.execute(); Now, i need to implement the "Cancel" button functionality, so i have to stop the execution of the running…
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
64
votes
2 answers

AsyncTask: where does the return value of doInBackground() go?

When calling AsyncTask, where is the return value of: protected Boolean doInBackground(Integer... params)? Usually we start AsyncTask with new AsyncTaskClassName().execute(param1,param2......); but it doesn't appear to…
Dhruv
  • 1,129
  • 2
  • 13
  • 32
62
votes
2 answers

AsyncTask execute() or executeOnExecutor()?

What's the difference between using execute() and executeOnExecuter()? How does execute() execute tasks by default? (in serial or in parallel?) What should be used for new SDKs >16? Is it a good practice to use parallel execution…
Rashad.Z
  • 2,494
  • 2
  • 27
  • 58
62
votes
2 answers

Android UI Thread Message Queue dispatch order

While working with retain Fragments in Android to hold an AsyncTask during configuration changes, which i guess it's the best approach, some doubts appear in my mind about UI Thread's Message Queue invocation order. Ex: Imagine this…
Sergio Serra
  • 1,479
  • 3
  • 17
  • 25
61
votes
6 answers

Difference between AsyncTask and Thread/Runnable

I have question which puzzles me. Imagine I wanna do something in another thread, like fetching GPS/Location stuff, which as recommended in the SDK documents, must use a background thread. So here is the question: What's the difference between…
60
votes
6 answers

How to stop asynctask thread in android?

I want to stop a AsyncTask thread from another AsyncTask thread. I have tried like new AsyncTask.cancel(true) to stop the background process but it didn't stop. Could any one help me on this?
user
  • 673
  • 1
  • 6
  • 10