我在我的應用程序中有許多活動(例如A-> B-> C-> D)...我有一個倒計時器,用於會話超時..我所做的是創建一個靜態計數器...我在活動A開始計數器....如果用戶交互計數器被複位...這也適用於活動B,C,D ....也對完成活動D,活動一開始..因爲這我已經使用addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
,所以它清除堆棧...在android中處理靜態變量
但是會發生什麼當活動A重新開始..一個新的實例與前一個計數器一起創建,並繼續運行在背景....並沒有重置userinteration ...我已經在onDestroy中做了counter = null
....是對的還是我需要做點別的?
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;
}
}
所以你建議我必須爲每個活動使用一個單獨的計數器? – Audi