Android UI Thread Message Queue dispatch order

It is not possible for onPostExecute() to be called in between Fragment#onDetach() and Fragment#onAttach() during a configuration change. The reasoning behind this claim is threefold: Configuration changes are handled inside a single message in the main thread’s message queue. As soon as the doInBackground() method returns, the AsyncTask schedules the onPostExecute() method to be invoked … Read more

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

The value is then available in onPostExecute which you may want to override in order to work with the result. Here is example code snippet from Google’s docs: private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground(URL… urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i … Read more

ProgressDialog in AsyncTask

/** * this class performs all the work, shows dialog before the work and dismiss it after */ public class ProgressTask extends AsyncTask<String, Void, Boolean> { public ProgressTask(ListActivity activity) { this.activity = activity; dialog = new ProgressDialog(activity); } /** progress dialog to show user that the backup is processing. */ private ProgressDialog dialog; /** application … Read more

iOS/Objective-C equivalent of Android’s AsyncTask

Answer to Original Question: Grand Central Dispatch (GCD) offers a mechanism to perform tasks in the background, though it works in a structurally different way than AsyncTask. To perform something asynchronously, you just need to create a queue (like a thread) and then pass a block to dispatch_async() to be performed in the background. I … Read more

How can you pass multiple primitive parameters to AsyncTask?

Just wrap your primitives in a simple container and pass that as a parameter to AsyncTask, like this: private static class MyTaskParams { int foo; long bar; double arple; MyTaskParams(int foo, long bar, double arple) { this.foo = foo; this.bar = bar; this.arple = arple; } } private class MyTask extends AsyncTask<MyTaskParams, Void, Void> { … Read more

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

Fragments can actually make this a lot easier. Just use the method Fragment.setRetainInstance(boolean) to have your fragment instance retained across configuration changes. Note that this is the recommended replacement for Activity.onRetainnonConfigurationInstance() in the docs. If for some reason you really don’t want to use a retained fragment, there are other approaches you can take. Note … Read more

getting context in AsyncTask

You need to do following things. when you want to use AsyncTask, extend that in other class say MyCustomTask. in constructor of new class, pass Context Example public class MyCustomTask extends AsyncTask<Void, Void, Long> { private Context mContext; public MyCustomTask (Context context){ mContext = context; } //other methods like onPreExecute etc. protected void onPostExecute(Long result) … Read more