Reputation: 2684
I have an AsyncTask running a loop which only stops looping when exiting the app, a global "stop" boolean gets set and it stops the loop and finishes through the AsyncTask.
I have this code:
@Override
public void onBackPressed()
{
KillAllThreads();
}
@Override
public void onUserLeaveHint()
{
KillAllThreads();
}
Now here is the thing. If I initiate the AsyncTask, onUserLeaveHint() gets called right away, and when the home button is pressed, it never fires this method. If I dont initiate the AsyncTask and let the activity load without doing anything, then when I press Home, it fires the onUserLeaveHint() method.
How am I supposed to stop the thread if the user clicks out of the app?
Upvotes: 1
Views: 1840
Reputation: 5542
You should be able to call .cancel() on a AsyncTask, have you tried that?
Have a look at this sample project on my google docs, It illustrates the important aspects of AsyncTask:
link:
Upvotes: 2