I have an AsyncTask running and in this thread I start a runOnUiThread to manipulate my views. If I close my AsyncTask, the runOnUiThread runs still 1 time longer after I closed my AsyncTask. How can I close the runOnUiThread? Thanks!
@Override
protected String doInBackground(String... params) {
while(running.get()) {
// Background thread sleeps
try {
Thread.sleep(produceRadwaste_countdown * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// UI thread touching some view elements
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
radwasteTon.incrementStack();
}
});
// Delete thread if player is game over
if(GameLogic.isGameOver()) {
clearThreads();
}
}
return "Running";
}
public void clearThreads() {
running.set(false);
this.cancel(true);
}
Can I propose different solution?
Implement onProgressUpdate(Params...) method in AsyncTask. Then you can call publishProgess from doInBackground(Params...);
onProgressUpdate(Params...) will be run on ui thread.