onPause/onResume activity issues

Posted by Josh on Stack Overflow See other posts from Stack Overflow or by Josh
Published on 2010-03-14T04:08:08Z Indexed on 2010/03/14 4:15 UTC
Read the original article Hit count: 594

Filed under:
|
|

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()
{
    super.onResume();
    Log.v("Resuming", String.format("Resuming with %d", this._timer_time_remaining));

    if(this._timer_time_remaining > 0) {
        setContentView(R.layout.in_game);
        start_timer(this._timer_time_remaining);
    }
}

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!

© Stack Overflow or respective owner

Related posts about android-sdk

Related posts about android