I have a small test application I am working on which has a timer that updates a textview to countdown from 100 to 0. That works fine, but now I am trying to pause the application if the user presses the back button on the phone and then restart the timer from where they left off when they reopen the app. Here is the code I am using:
@Override
public void onPause()
{
if(this._timer_time_remaining > 0) {
this.timer.cancel();
}
super.onPause();
Log.v("Pausing", String.format("Pausing with %d", this._timer_time_remaining));
}
@Override
public void onResume()
{
Log.v("Resuming", String.format("Resuming with %d", this._timer_time_remaining));
if(this._timer_time_remaining > 0) {
start_timer(this._timer_time_remaining);
}
super.onResume();
}
The start_timer() method creates a CountDownTimer which updates the textview in the onTick method and updates the this._timer_time_remaining int variable.
CountDownTimer and _timer_time_remaining are both declared at the class level like this:
private CountDownTimer timer;
private int _timer_time_remaining;
From the Log.v() prints I see that the _timer_time_remaining variable has the correct number of seconds stored when onPause is called, but it is set back to 0 when onResume starts. Why does the variable get reset? I thought that the application would continue to run in the background with the same values. Am I missing something? This is all declared in a class that extends Activity.
Thanks in advance!
Note: Edited to clean up the code copying