Reputation: 3790
I am properly executing a timed task on activity as follows. However, when activity onResume, an exeption is thrown, "Timer Task is scheduled already". I cancel timer once task is executed. How to solve it? thank you
final Runnable setButton = new Runnable() {
public void run() {
myClass.aBridge.button_back.setVisibility(View.INVISIBLE);
timer.cancel();
}
};
TimerTask task = new TimerTask(){
public void run() {
webPush.this.runOnUiThread(setButton);
}
};
@Override
protected void onResume() {
super.onResume();
timer = new Timer();
timer.schedule(task, 5000);
}
task is called once before onResume as:
timer = new Timer();
timer.schedule(task, 5000);
Upvotes: 0
Views: 424
Reputation: 1352
You can only call timer.schedule() one time for each TimerTask instance. Create a new instance before you schedule it.
edit: for your code, don't initialize the TimerTask member variable at the point where you define it. Instead, create a new instance in your onResume(), right before you schedule it.
Upvotes: 2