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,
Probably it can help to someone who will meet this in future. You can just add a method:
public class MainActivity extends Activity {
public static MyTask loadTextDataTask;
//... methods
public class MyTask extends AsyncTask<Void, Void, Void> {
//... methods
public void selfRestart() {
loadTextDataTask = new MyTask();
}
}
}
Then you can use it from any other class like:
MainActivity.loadTextDataTask.selfrestart();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With