我的应用程序中有很多活动(例如 A->B->C->D)...我有一个用于会话超时的倒数计时器。我所做的是创建了一个静态计数器...我在活动 A 处启动计数器......如果用户交互计数器被重置......这也适用于活动 B、C、D....同样在完成活动 D 时,活动 A 开始......为此我已经使用addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
所以它清除堆栈...
但是当活动 A 再次启动时会发生什么......一个新实例与前一个计数器一起创建并继续在后台运行......并且不会在用户交互时重置......我已经counter = null
在 onDestroy 中完成......是对的还是我需要做其他事情???
public class CountTime extends Activity {
TextView tv;
static MyCount counter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
// 5000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
counter = new MyCount(5000, 1000);
counter.start();
}
@Override
public void onUserInteraction() {
// TODO Auto-generated method stub
super.onUserInteraction();
counter.start();
}
// countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText("Left: " + millisUntilFinished / 1000);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
counter = null;
}
}