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
答案 0 :(得分:0)
AsyncTasks按照收到的顺序在单个线程上运行。如果在AsyncTask完成之前退出,它将继续运行。如果在完成之前重新启动它,它将在任务队列中等待,直到原始运行完成才能运行。您可以通过告诉它在自己的队列上运行来避免它。虽然一个更好的方法可能是一个Loader,如果它仍然会获取相同的数据(这将允许它不再运行第二次但使用第一次运行的结果)。