You need to stop the while loop somehow. Maybe set a class flag to false in onDestroy
and the while loop becomes while(flag)
:
public class MyTimerTask extends TimerTask {
@Override
public void run() {
flag = true;
while(flag && (getActivity() != null))
{try {
Thread.sleep(1000);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
updateTextView();
}
});
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
And your onDestroy
method would look like:
public void onDestroy() {
flag = false;
super.onDestroy();
}
Don't forget to declare the flag
in your fragment class:
private boolean flag = false;