Android restart AsyncTask

Viewed 13255

I am having some trouble restarting an AsyncTask after I try to reopen the activity.

When I first open the activity I call this to start the AsyncTask which works the very first time.

myTask connectedTask;
connectedTask = new myTask();
connectedTask.execute();

public class myTask extends AsyncTask<Integer,Integer, Integer> {

    @Override
    protected Integer doInBackground(Integer... arg0) {
        //Increase timer and wait here in a loop
        System.out.println("ASYNC TASK STARTING!");
        return IsSocketConnected();
    }

    protected void onPostExecute(Integer result) {
        //Something you want to do when done?
        System.out.println("ASYNC TASK DONE!");

        // if it was connected successfully 
        if(result == 1) {
            // remove the progress bar
            proBar.setVisibility(View.GONE);

            // Discover available devices settings and create buttons
            CreateButtons(btnList);
        }
    }
}

IsSocketConnected(); // checks for a bluetooth connections to be done. 

When I go back to previous activity and try to start that activity again I can't get the AsyncTask to start again.

I read that as long as I create a new instance of the AsyncTask I should be to restart those tasks.

Is there something else that I should be doing?

Thank you,

7 Answers
Related