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
38
votes
2 answers

Android Retrofit - onProgressUpdate for showing Progress Notification

I'm currently using Retrofit by Square for Android network communications. Is there a way to get its progress during a task to create a progress notification, something similar to that which Facebook uses when uploading an image? Use Case would be…
John Shelley
  • 2,655
  • 3
  • 27
  • 36
37
votes
3 answers

Example: Android bi-directional network socket using AsyncTask

Most of the network socket examples I found for Android were one directional only. I needed a solution for a bi-directional data stream. I eventually learned of the AsyncTask. This example shows how to get data from a socket and send data back to…
Lance Lefebure
  • 6,845
  • 6
  • 24
  • 18
37
votes
2 answers

Should I call super.onPostExecute(result) in Android AsyncTask?

I wonder if it has any meaning to call super.onPostExecute(result) or super.onPreExecute in Android AsyncTask? I have been always calling them, but even in Android documentation about AsyncTask (Android API Reference: AsyncTask) they are omitted.…
Andranik
  • 2,729
  • 1
  • 29
  • 45
36
votes
5 answers

Android: RunOnUiThread vs AsyncTask

I believe Google suggests developers to use AsyncTask. However, I would like to know how is it different from using 'new Thread' and then calling 'RunOnUiThread' in performance and memory efficiency. Example for using RunOnUithread: // some code…
jclova
  • 5,466
  • 16
  • 52
  • 78
36
votes
5 answers

Android how to group async tasks together like in iOS

I have a function in iOS app that uses dispatch_group to group multiple rest request: static func fetchCommentsAndTheirReplies(articleId: String, failure: ((NSError)->Void)?, success: (comments: [[String: AnyObject]], replies: [[[String:…
OMGPOP
  • 1,995
  • 8
  • 52
  • 95
36
votes
4 answers

Android : Loading an image from the Web with Asynctask

How do I replace the following lines of code with an Asynctask ? How do you "get back" the Bitmap from the Asynctask ? Thank you. ImageView mChart = (ImageView) findViewById(R.id.Chart); String URL = "http://www...anything…
Hubert
  • 16,012
  • 18
  • 45
  • 51
36
votes
3 answers

OkHttp Library - NetworkOnMainThreadException on simple post

I want to use OkHttp library for networking in Android. I started with the simple post example as written in their website: public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); OkHttpClient client = new…
Aviv Ben Shabat
  • 1,073
  • 2
  • 13
  • 33
36
votes
5 answers

What is the different between Handler, Runnable, and Threads?

What is the difference between Handler, Runnable, and Threads? While I was working with android, and I need something to run in the background. I use Threads to run it. Usually I would write a class that extends Thread and implement the run…
Hong Wei Wang
  • 1,388
  • 3
  • 19
  • 29
34
votes
7 answers

AsyncTask doInBackground does not run

I'm having a problem with the AsyncTask class. It seems like my task stops working after creating 4 or 5 tasks. Im having 2 activities. MainActivity which only holds a button that starts a second activity called ImageActivity. ImageActivity is…
Vidar Vestnes
  • 42,644
  • 28
  • 86
  • 100
34
votes
5 answers

When to use a Service or AsyncTask or Handler?

Can someone tell me the TRUE difference?
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
34
votes
3 answers

How to check if Async Task is already running

I have an app that needs to do an intensive database operation on start up. The app holds a local copy of the contacts on the phone and synchronizes with the android contact database on startup. If a user starts the app, an Async Task is started…
Kevin Bradshaw
  • 6,327
  • 13
  • 55
  • 78
33
votes
5 answers

AsyncTask.executeOnExecutor() before API Level 11

The normal way we do AsyncTask in Android is, from Android API: private class DoIntenseTask extends AsyncTask { protected Void doInBackground(Object... params) { for (Object param : params) { Object rtnObj =…
yorkw
  • 40,926
  • 10
  • 117
  • 130
33
votes
4 answers

Android AsyncTask context behavior

I've been working with AsyncTasks in Android and I am dealing with an issue. Take a simple example, an Activity with one AsyncTask. The task on the background does not do anything spectacular, it just sleeps for 8 seconds. At the end of the…
dnkoutso
  • 6,041
  • 4
  • 37
  • 58
33
votes
2 answers

Garbage Collection causes : MediaPlayer finalized without being released

After a lot of debugging I finally found what is causing this error! Garbage Collection! I have a video playing in media view and in the background I am looking for new videos from a Rest API. Every now and then I see the Garbage collection…
Harry
  • 13,091
  • 29
  • 107
  • 167
32
votes
5 answers

Android HTML ImageGetter as AsyncTask

Okay, I'm losing my mind over this one. I have a method in my program which parses HTML. I want to include the inline images, and I am under the impression that using the Html.fromHtml(string, Html.ImageGetter, Html.TagHandler) will allow this to…
Nick
  • 6,900
  • 5
  • 45
  • 66