Android onPause/onResume 活动问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2441145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onPause/onResume activity issues
提问by Blather
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:
我有一个我正在开发的小型测试应用程序,它有一个计时器,可以将 textview 从 100 更新为从 100 到 0 的倒计时。这很好用,但现在我试图暂停应用程序,如果用户按下手机上的后退按钮,然后当他们重新打开应用程序时,从他们停止的地方重新启动计时器。这是我正在使用的代码:
@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.
start_timer() 方法创建一个 CountDownTimer,它更新 onTick 方法中的 textview 并更新 this._timer_time_remaining int 变量。
CountDownTimer and _timer_time_remaining are both declared at the class level like this:
CountDownTimer 和 _timer_time_remaining 都是在类级别声明的,如下所示:
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.
从 Log.v() 打印中我看到 _timer_time_remaining 变量在 onPause 被调用时存储了正确的秒数,但在 onResume 启动时它被设置回 0。为什么变量会被重置?我认为该应用程序将继续以相同的值在后台运行。我错过了什么吗?这一切都在扩展 Activity 的类中声明。
Thanks in advance!
提前致谢!
Note: Edited to clean up the code copying
注意:编辑以清理代码复制
回答by Nikola Smiljani?
If you take a look at the diagram for Activity lifecycleyou'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 onSaveInstanceStateand restore it using onRestoreInstanceState.
如果您查看Activity 生命周期的图表,您会意识到在调用 onPause 之后无法保证您的 Activity。Android 甚至可以在不调用 onDestroy 的情况下杀死您的 Activity。您必须使用onSaveInstanceState保存您的状态并使用onRestoreInstanceState恢复它。
Thismight 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 storagefor this.
生命周期涵盖不同的场景。如果您只有一个 Activity,则按 Back 相当于退出您的应用程序。如果您有启动活动 B 的活动 A,则为活动 A 调用 onSaveInstanceState。需要为此使用某种持久存储。
回答by user425923
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.
我从我的一些工作代码中改编了这个,但实际上并没有以这种确切的格式对其进行测试。搜索 sharedPreferences 的文档以了解其工作原理。
回答by ashoke
I'm not an expert on Android, but just telling from how I expect the timer to behave
我不是 Android 方面的专家,但只是从我期望计时器的行为方式来说明
this.timer.cancel()
:- would cancel the timer, but not pause it
this.timer.cancel()
:- 会取消计时器,但不会暂停它
start_timer(this._timer_time_remaining)
:- would "restart" the timer, not continue from the last position.
start_timer(this._timer_time_remaining)
:- 会“重新启动”计时器,而不是从最后一个位置继续。
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).
所以你看到的是正确的。每次时间都是重新开始。找出暂停计时器的 api(如果有)。
回答by Juan
You can also try listening to the to key presses ( onKeyDown() ) to intercept the back button and save your data to a SQLite table.
您还可以尝试收听按键( onKeyDown() )以拦截后退按钮并将您的数据保存到 SQLite 表中。
回答by James
You can simply write the time out to a database or file in onPause and read it back in onResume.
您可以简单地在 onPause 中将超时写入数据库或文件,然后在 onResume 中读回。