onPause/onResume activity issues

2019-01-22 20:50发布

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

标签: android timer
5条回答
我只想做你的唯一
2楼-- · 2019-01-22 21:32

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.

查看更多
Anthone
3楼-- · 2019-01-22 21:48

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

查看更多
等我变得足够好
4楼-- · 2019-01-22 21:51

You can also try listening to the to key presses ( onKeyDown() ) to intercept the back button and save your data to a SQLite table.

查看更多
▲ chillily
5楼-- · 2019-01-22 21:56

This is how I would save the value: (Four lines of code)

SharedPreferences example = getSharedPreferences("example", MODE_PRIVATE);
SharedPreferences.Editor edit = example.edit();
edit.putInt("time_remaining", (Integer)this._timer_time_remaining);
edit.commit();

On resume, do this: (Two lines of code)

SharedPreferences example = getSharedPreferences("example", MODE_PRIVATE);
int time_remaining = example.getInt("time_remaining", 0);

I adapted this from some of my working code, but did not actually test it in this exact format. Search documentation for sharedPreferences to learn how it works.

查看更多
Summer. ? 凉城
6楼-- · 2019-01-22 21:56

I'm not an expert on Android, but just telling from how I expect the timer to behave

this.timer.cancel() :- would cancel the timer, but not pause it

start_timer(this._timer_time_remaining) :- would "restart" the timer, not continue from the last position.

So what you see is correct. Every time the time is started afresh. Find out what is the api to pause the timer (if any).

查看更多
登录 后发表回答