Execute AsyncTask several times
AsyncTask
instances can only be used one time.
Instead, just call your task like new MyAsyncTask().execute("");
From the AsyncTask API docs:
Threading rules
There are a few threading rules that must be followed for this class to work properly:
- The task instance must be created on the UI thread.
- execute(Params...) must be invoked on the UI thread.
- Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
- The task can be executed only once (an exception will be thrown if a second execution is attempted.)
Android AsyncTask called multiple times
It literally means, that while AsyncTask
is running, you cannot launch it again.
In your MainActivity.class
you have line:
task.execute();
if task either is finished or not and you call the method again then exception
will be thrown.
And put this method in onResume
() is a good practice.
Only one thing to notice is: if you put in onRestart
() remember, this callback works when you change configuration, but it will not be called if you Create activity
.
The doc about lifecycle of an Activity.
execute asynctask multiple times or parallel in a loop
Create individual instance of AsyncTask
inside the for loop.
for(..){
//
new MyAsyncTask().execute(obj.getString("source"));
}
Running same Asynctask multiple times sequentially
You could use a field variable in the outer class:
private int count;
private void attemptConnect(){
count = 0;
new MyTask().execute("");
}
class MyTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
return null;
}
@Override
protected void onPostExecute(String s) {
count++;
if(count < 5){
new MyTask().execute("");
}
}
}
Or pass in a count to the AsyncTask constructor:
private void attemptConnect(){
new MyTask(0).execute("");
}
class MyTask extends AsyncTask<String, String, String> {
private int count;
public MyTask(int count){
this.count = count;
}
@Override
protected String doInBackground(String... params) {
return null;
}
@Override
protected void onPostExecute(String s) {
if(count < 4){
new MyTask(++count).execute("");
}
}
}
AsyncTask and error when executed twice
No. You cannot execute the same async task twice. You are able to do it again after 30 secs because the async task completes processing and returns the result from doInBackground(). Hence you need to create an object every time or use a progress dialog to block the user from clicking on the button again. Show the progress dialog in onPreExecute() and dismiss in onPostExecute().
Prevent asynctask from being executed multiple times
There are two Ways to do this.
1: Show a Progress Dialog and block the user for further input.
2: Create you AsyncTask variable in class scope private. Then this will never run more than once.
Is it possible to run multiple AsyncTask in same time?
use executor as follows
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new Save_data().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, location);
} else {
new Save_data().execute(location);
}
See this
Related Topics
How to Handle an Asynctask During Screen Rotation
Accessing Localhost of Pc from Usb Connected Android Mobile Device
Why Use Fragment#Setretaininstance(Boolean)
How to Change the Color of a Part of a Textview
Zxing Camera in Portrait Mode on Android
How to Set Custom Header in Volley Request
Android - How to Receive Broadcast Intents Action_Screen_On/Off
Replace Selector Images Programmatically
How to Call a .Net Webservice from Android Using Ksoap2
Android Turn On/Off Wifi Hotspot Programmatically
How to Set Image Button Backgroundimage for Different State
How to Bring an Activity to Foreground (Top of Stack)
Difference Between Android.App.Fragment and Android.Support.V4.App.Fragment
Custom Getfilter in Custom Arrayadapter in Android
Published App on Play Store Can't Communicate with Google Maps API and Facebook API
The Number of Method References in a .Dex File Cannot Exceed 64K API 17