0

I have an AsyncTask which is implemented as follows:

   AsyncTask asyncTask = new AsyncTask() {
                @Override
                protected Object doInBackground(Object[] params) {
                     //downloading images from google places api
                }

                @Override
                protected void onPostExecute(Object o) {

                    }
                }
            };
            asyncTask.execute(); 

This AsyncTask is triggered to run multiple times and it's executed by the scope of the doInBackground() method of another AsyncTask. It runs fine the first time I load an activity, and if I let all the data download and then exit the activity (so that onStop() is called) and reopen the activity it also runs fine then, but, if I exit the activity before all the data is downloaded it won't run when I reopen the activity.

Does anyone know why this is? Can anyone recommend a better approach that doesn't involve AsynTask?

Note: I have tried using executeOnExecutor() but it hasn't solved the issue.

Thanks in advance

Micah Simmons
  • 2,078
  • 2
  • 20
  • 38
  • In which method you are starting the task? Also, how are you leaving and re-entering the activity ? – Shadab Ansari Apr 21 '16 at 22:41
  • I'm starting the task in a method which is called in the doInBackground() method of another Task. The reason I am leaving and re-entering the activity is to simulate a user iterating through different screens in the app – Micah Simmons Apr 21 '16 at 22:49
  • In which method, you are starting the first Task? – Shadab Ansari Apr 21 '16 at 22:50
  • The order is like this: getLocation >queryMapsApiTask > getMainJSONObjectTask>methodToExtractJSONData>getPicturesTask, where the last method (the getPicturesTask) is the task that is not executing – Micah Simmons Apr 21 '16 at 22:55
  • Doesn't seems a good idea using tasks within tasks. Anyways, check out this - http://stackoverflow.com/questions/10048958/android-calling-asynctask-right-after-an-another-finished – Shadab Ansari Apr 21 '16 at 23:00
  • I looked at the post. But, the tasks run when I first load the activity, it is only if I leave the activity before all the tasks are finished and then re-enter the activity the last task won't run – Micah Simmons Apr 21 '16 at 23:07
  • Do you know of any better alternatives to AsyncTask ? – Micah Simmons Apr 21 '16 at 23:07
  • I guess you are not starting your first task within onCreate() ? – Shadab Ansari Apr 21 '16 at 23:11
  • I don't start the first task within Oncreate(). The first task gets called inside the getLocation() method which is called by the onMapReady() callback of the OnMapReadyCallback interface – Micah Simmons Apr 21 '16 at 23:15

1 Answers1

0

AsyncTasks run on a single thread, in the order received. If you exit before an AsyncTask finishes, it continues to run. If you relaunch it before it finishes, it waits in the task queue until the original run is finished before it can be run. You can avoid it by telling it to run on its own queue. Although a better method may be a Loader if it will be fetching the same data anyway (that would allow it not to run a second time but use the results of the first run).

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks for your response, and sorry for my delayed reply. The loader suggestion wasn't suitable in my case as I was looking for fresh data everytime the activity was created. The trouble was, the pending AsyncTasks triggered via an Activity being launched weren't waiting in the task queue until the original AsyncTasks were finished, or at least seem not to have been, as no matter how long the activity was left they never executed. If by run on its own queue you mean use a version of executeOnExecutor(), I had already tried that, but it didn't work. Is that what you were suggesting? – Micah Simmons Apr 28 '16 at 09:24