onPause/onResume 活动问题 [英] onPause/onResume activity issues

查看:29
本文介绍了onPause/onResume 活动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小型测试应用程序,它有一个计时器,可以将 textview 从 100 更新为从 100 到 0 的倒计时.这很好用,但现在我正尝试在用户按下后退按钮时暂停应用程序手机,然后从他们重新打开应用程序时停止的地方重新启动计时器.这是我正在使用的代码:

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();
}

start_timer() 方法创建一个 CountDownTimer,它更新 onTick 方法中的 textview 并更新 this._timer_time_remaining int 变量.

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 和 _timer_time_remaining 都在类级别声明如下:

CountDownTimer and _timer_time_remaining are both declared at the class level like this:

private CountDownTimer timer;
private int _timer_time_remaining;

从 Log.v() 打印中我看到 _timer_time_remaining 变量在调用 onPause 时存储了正确的秒数,但在 onResume 启动时它被设置回 0.为什么变量会被重置?我认为该应用程序将继续以相同的值在后台运行.我错过了什么吗?这一切都在一个扩展 Activity 的类中声明.

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.

提前致谢!

注意:编辑以清理代码复制

Note: Edited to clean up the code copying

推荐答案

如果你看一下 Activity 生命周期 你会意识到在 onPause 被调用之后你的 Activity 没有任何保证.Android 甚至可以在不调用 onDestroy 的情况下杀死您的 Activity.您必须使用 onSaveInstanceState<保存您的状态/a> 并使用 onRestoreInstanceState<恢复它/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.

可能有用.

生命周期涵盖不同的场景.如果您只有一个 Activity,则按 Back 相当于退出您的应用程序.如果您有启动活动 B 的活动 A,则为活动 A 调用 onSaveInstanceState.需要使用某种持久存储为此.

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.

这篇关于onPause/onResume 活动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆