How to run the same asynctask more than once?

Viewed 56361

I have my asyncTask run when the activity first starts, then if network connectivity is not available then i have a refresh button that tries to run the asyncTask to try again. But i get a debug error saying this..

07-29 18:14:21.290: ERROR/AndroidRuntime(9080): FATAL EXCEPTION: main
07-29 18:14:21.290: ERROR/AndroidRuntime(9080): java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
   07-29 18:14:21.290: ERROR/AndroidRuntime(9080):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:541)
   07-29 18:14:21.290: ERROR/AndroidRuntime(9080):     at android.os.AsyncTask.execute(AsyncTask.java:499)
  07-29 18:14:21.290: ERROR/AndroidRuntime(9080):     at com.fttech.gameIT.MainMenu$1.onClick(MainMenu.java:90)

Is there anyway to run this twice?

13 Answers

This solved my problem:

public class MainActivity extends AnimationActivity {

    MyAsyncTasks asyncTasks = new MyAsyncTasks();

    @BindView(R.id.refresh_btn)
    Button refreshBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUnbinder(ButterKnife.bind(this));  // ButterKnife usage

        syncTasks();  // run asyncTasks on activity start

        refreshBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                syncTasks(); // run asyncTasks on button click
            }
        });
    }

    private void syncTasks() {
        try {
            if (asyncTasks.getStatus() != AsyncTask.Status.RUNNING){   // check if asyncTasks is running
                asyncTasks.cancel(true); // asyncTasks not running => cancel it
                asyncTasks = new MyAsyncTasks(); // reset task
                asyncTasks.execute(); // execute new task (the same task)
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("MainActivity_TSK", "Error: "+e.toString());
        }
    }
}

You can do it like this :

private MyAsyncTask createAsyncTask(){
    if (myAsyncTask == null){
        return myAsyncTask = new MyAsyncTask();
    }
        myAsyncTask.cancel(true);
        return myAsyncTask = new MyAsyncTask();
 }

and then you can use it :

createAsyncTask().execute();

this make a new instance of your background task everytime.

Async tsk only run once as the Exceptions says the task has already been executed.. So you just have to make a new instance of async and then call .execute(); in order to run it again .. thank you

You could cancel your asyncTask when you press the button and then execute it again.

Inside OnClic method:

asyncTask.cancel();
AsyncTask asyncTask = new AsyncTask();
asyncTask.execute();

@coder_For_Life22 I think am late for the answer ,anyway you can do like

    @Override
    protected void onPostExecute(Void a) {

        myAsyncTask=new MyAsyncTask();
    }

in order to start a new AsyncTask after execution :)

Related