I have a TimerTask running inside one of the fragment of my app; when I try to switch dinamically to another fragment using the "fragmentManager.beginTransaction().replace" method my app crashes. I'm sure that my issue is linked with the Timer bucause if I comment it everything works perfectly. Here is the Timer code inside my fragment:
public void startTimer(ProgressBar b){
final ProgressBar bar = b;
t = new Timer();
task = new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
bar.setMax(3600);
bar.setProgress(mytime);
//TextView tv1 = (TextView) getView().findViewById(R.id.textView2);
//tv1.setText("TIME LEFT:" +time);
if (mytime <= 3600)
mytime += 1;
else {
//tv1.setText("GAME OVER");
}
}
});
}
};
t.scheduleAtFixedRate(task, 0, 1000);
}
Maybe I can't change fragment while I'm running the TimerTask on the UiThread? Thanks for any answer.