tags:

views:

1096

answers:

2

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

+2  A: 

If you take a look at the diagram for Activity lifecycle you'll realize that there are no guarantees about you Activity after onPause is called. Android could kill you Activity without even calling onDestroy. You have to save your state using onSaveInstanceState and restore it using onRestoreInstanceState.

This might be useful.

EDIT:

Lifecycle covers different scenarios. If you have only one Activity pressing Back is equivalent to exiting your application. If you have Activity A which starts activity B then onSaveInstanceState is called for Activity A. When you press back in Activity B it just exits and onRestoreInstanceState is called for Activity A. You are trying to save data between two distinct runs of your application and you need to use some kind of persistent storage for this.

Nikola Smiljanić
First, thanks for the response.I replaced my onPause method with the onSaveInstanceState(Bundle savedInstancestate) method, but this doesn't seem to be called when I press the back button to leave my application. None of my LogCat messages show up so I don't think it is being called at all. Do I need onPause and onSaveInstanceState implemented? Or should onSaveInstanceState handle it?
Josh
Documentation explains this: "One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it."Pressing Back doesn't call onSaveInstanceState but pressing Home does.
Nikola Smiljanić
Thanks for responding. I did see that mentioned, but if onSaveInstanceState isn't called when the back button is pressed it won't take care of my issue, will it? I just want the count down timer to stop when the back button is pressed and then restart from the same location when the app is started again from the home screen. I may be misunderstanding, but it doesn't appear that this will accomplish that. Thanks again for the time you've taken to help so far, I am just missing something here I think.
Josh
You could save the time left in the preferences.
MrSnowflake
A: 

You can simply write the time out to a database or file in onPause and read it back in onResume.

James