I have many activities in my application(eg A->B->C->D)...i have a countdowntimer that i am using for a session timeout.. What i have done is created a static counter...I start the counter at activity A....if the user interacts the counter is reset...this also applies for activities B,C,D....also on finishing activity D, Activity A gets started..For this i have used addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) so it clears the stack...
but what happens is when the activity A gets started again..A new instance gets created along with the previous counter and keeps running in background....And is not reset on userinteration... i have done counter = null in onDestroy....is it right or i need to do something else???
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; } }