You cannot run the same AsyncTask instance more than once. Suppose you have an AsyncTask named MyAsyncTaks, and you intend to do something like this,
MyAsyncTask myAsyncTask = new MyAsyncTaks(); myAsyncTask.execute();
The reason for this is that a thread that once completed its run method cannot be assigned to another task. Here, when you execute execute () for the first time, your AsyncTask started working and after completing its work the thread crashes. Naturally, the next call to execute () will throw you an exception.
The easiest way to run this more than once is to create a new instance of MyAsyncTaks and execute it.
MyAsyncTask myAsyncTask = new MyAsyncTaks(); myAsyncTask.execute(); // Works as expected . . . MyAsyncTask myAsyncTask2 = new MyAsyncTaks(); myAsyncTask2.execute(); // Works as expected
Although you do not need to mention it here, you need to remember that after the Android SDK version of Honeycomb, if you run several Asynchronous tasks at once, they will actually start sequentially. If you want to run them in parallel, use executeOnExecutor instead.
Kaps Jan 29 '17 at 8:41 2017-01-29 08:41
source share