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
32
votes
4 answers

How to communicate between Firebase Messaging Service and Activity? Android

I know the question about how to communicate between a service and an activity has been answered many times but I also want my own way of doing this to be reviewed and to know if its an acceptable and the right way to do this and what are the…
32
votes
3 answers

onPostExecute on cancelled AsyncTask

Does onPostExecute execute if the AsyncTask has been cancelled? If it does execute, is it safe to say that I should always ask if the task has been cancelled (isCancelled) at the start of onPostExecute, before doing anything else?
hpique
  • 119,096
  • 131
  • 338
  • 476
32
votes
6 answers

Asynctask vs Thread vs Services vs Loader

I got slightly confused about the differences between Asynctask, Thread, Service, Loader in Android. I know how it works. But i still don't understand what and when should i use. I work with Android for 3 years, and generally still use AsyncTask…
32
votes
3 answers

ASyncTasks blocking others

I've 2 ASyncTasks, one retrieves a value from an httpPost and the other update some elements of the UI (including an listview). The problem is that since both ASyncTasks share the same background thread, if the network opperation start first and…
Addev
  • 31,819
  • 51
  • 183
  • 302
31
votes
1 answer

Android 3.0 - what are the advantages of using LoaderManager instances exactly?

With 3.0 we got the fancy LoaderManager, which handles data loading using the AsyncTaskLoader, the CursorLoader, and other custom Loader instances. But reading through the docs for these I just couldn't get the point: how are these better than just…
31
votes
5 answers

NetworkOnMainThreadException

I just found out about NetworkOnMainThreadException at official docs and was wondering if the emulator is throwing this. I have been testing my app quite a bit and as far as I know all networking is off the main thread (using Roboguice…
31
votes
2 answers

AsyncTask and Looper.prepare() error

I have the following code class OverlayTask extends AsyncTask { @Override public void onPreExecute() { if (sites != null) { myMapView.getOverlays().remove(sites); myMapView.invalidate(); …
Lee Armstrong
  • 11,420
  • 15
  • 74
  • 122
31
votes
2 answers

Using OKHttp, what is the difference between synchronous request in AsyncTask and OKhttp Asynchronous request?

OKHttp supports both synchronous and asynchronous api. If I want to issue an async request, I can: Use a AsyncTask, and issue OKhttp synchronous api. Issue a OKhttp asynchronous api. What is the difference between these 2 options? And which one…
NeoWang
  • 17,361
  • 24
  • 78
  • 126
31
votes
4 answers

What's the correct way to implement AsyncTask? static or non static nested class?

The "Login" from Android examples implemented AsyncTask as a non-static inner class. However, according to Commonsguys, this class should be static and use weak-reference to the outside activity see this. So what is the correct way to implement…
roxrook
  • 13,511
  • 40
  • 107
  • 156
31
votes
2 answers

Executing Multiple AsyncTask's Parallely

I'm using Android SDK 4.0.3 API15 and I want to run multiple AsyncTasks parallely. I'm getting my data from web server and animating(10 fps) it real-time. But depending on user operations I need to send some data to web server also. When this occurs…
Srht
  • 473
  • 3
  • 7
  • 17
30
votes
4 answers

Simulate slow network on Android simulator

I'm building an app that uses AsyncTask to display a progress bar when it's performing network operation (Google translate). However, the problem is that I can't tell if it's working since the network is too fast and it finishes running the…
Charles Li
  • 1,835
  • 3
  • 24
  • 40
30
votes
4 answers

AsyncTask ; doInbackground() not called after calling method onPreExecute()

I did added async library at my project and checked already, I don't know why code flow doesn't go in to asynctask Code public void doMysql() { Log.v("doMysql", "accessed"); new AsyncTask() { @Override …
LKM
  • 2,410
  • 9
  • 28
  • 53
30
votes
7 answers

Android threading and database locking

We are using AsyncTasks to access database tables and cursors. Unfortunately we are seeing occasional exceptions regarding the database being locked. E/SQLiteOpenHelper(15963): Couldn't open iviewnews.db for writing (will try…
Pandalover
  • 2,388
  • 3
  • 21
  • 19
30
votes
0 answers

ObjectAnimator onAnimationEnd listener is called before the animation finishes

its weird and strange, but it seems like when I attach an AnimatorListener to an ObjectAnimator I use for animating fragment transactions, the callback gets actually called slightly before finish of the animation. I use the listener to populate…
30
votes
3 answers

Android ICS and MJPEG using AsyncTask

I modified the MJPEG viewer code from Android and MJPEG to work using an AsyncTask (and thus work on Ice Cream Sandwich (ICS), 4.0.4) and here is my code. If anyone has any suggestions on how to optimize, cleanup, or do something more proper with…
bbodenmiller
  • 3,101
  • 5
  • 34
  • 50