简体   繁体   中英

onPause/onResume activity issues

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

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.

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.

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).

您可以简单地将时间写入onPause中的数据库或文件,并在onResume中将其读回。

您还可以尝试侦听按键(onKeyDown())来拦截后退按钮并将数据保存到SQLite表中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM