Reputation: 5407
have started my timer from the onCreate method. Even If My activity stops ,Timer keeps Running. How it is possible?
Toast in onTick() method diplays, Thread is main thread. I thought , when Activity stops Main thread will stop. can anyone give the explanation about this.
CustomCountDownTimer.java
private class CustomCountDownTimer extends CountDownTimer
{
public CustomCountDownTimer(long millisInFuture,long countDownInterval)
{
super(millisInFuture,countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
long seconds=millisUntilFinished/1000;
long hours=seconds/(60*60);
long remainingSeconds=seconds%(60*60);
long minutes=remainingSeconds/60;
remainingSeconds=remainingSeconds%60;
Toast.makeText(HomeActivity.this, Thread.currentThread().getName()+"", Toast.LENGTH_LONG).show();
btnStatusMessage.setText(hours+" hr "+minutes+" min ");
}
Upvotes: 4
Views: 5653
Reputation: 4132
just add to your activity
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
mHandler.removeCallbacks(mUpdateTimeTask);//mHandler (Handler) and mUpdateTimeTask (Runnable)
}
dont forget to add this too:
mHandler.removeCallbacks(mUpdateTimeTask);
before you call the intent too
home this will help you
Upvotes: 0
Reputation: 4425
1.Use flage to check timer is already running
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
btnStatusMessage.setText("STOP");
} else {
countDownTimer.cancel();
timerHasStarted = false;
btnStatusMessage.setText("RESTART");
}
Upvotes: 0
Reputation: 3700
You should add this code to your activity.
public void onDestroy(){
super.onDestroy();
timer.cancel();
}
Upvotes: 1
Reputation: 3527
The timer is not related to the Activity. You need to stop it in one of the Activity's state methods (onPause/onStop/etc').
Upvotes: 1