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

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

问题描述

我有一个小的测试应用程序,我的工作有一个更新的TextView从100倒数至0,即正常工作的计时器,但现在我想暂停应用程序,如果用户presses背在手机上按钮,然后重新启动,从他们离开的地方,当他们重新打开应用程序的计时器。这里是code我使用的:

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其更新的TextView在onTick方法并更新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被称为存储正确的秒数,但它重新设置为0 onResume启动时。为什么变量会马上重新设置?我认为,应用程序将继续在具有相同价值观的后台运行。我失去了一些东西?这是在扩展活动一类的所有声明。

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.

在此先感谢!

请注意:编辑以清理code复制

Note: Edited to clean up the code copying

推荐答案

如果你看一看的示意图的 你的活动周期将认识到,有没有对你保证在onPause被称为活动后。机器人可以杀死你的活动,甚至没有要求的onDestroy。你必须保存你的国家使用<一个href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29">onSaveInstanceState并使用恢复<一个href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29">onRestoreInstanceState.

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.

<一个href="http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state">This可能是有用的。

编辑:

生命周期涵盖了不同的方案。如果你只有一个活动pressing返回等同于退出应用程序。如果你有一个活动而开始活动B,那么的onSaveInstanceState被调用活动答:当您preSS早在b活动,它只是退出和onRestoreInstanceState被调用活动答:您正在尝试两种不同的运行间保存数据的应用程序,您需要使用某种形式的<一个href="http://developer.android.com/intl/de/reference/android/app/Activity.html#SavingPersistentState">persistent存储这一点。

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天全站免登陆