问题的理解生命周期时

问题的理解生命周期时

本文介绍了问题的理解生命周期时,屏幕熄灭,在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

信息:我的设备是Nexus One的2.2和我已经测试了两个项目,一个在1​​.5和一个在2.1。

问题:我有麻烦理解我的应用程序的生命周期中,当屏幕处于关闭状态而上。

下面是我的输出

  //活动开始
08-04 17:24:17.643:ERROR / PlayActivity(6215):ONSTART执行...
08-04 17:24:17.643:ERROR / PlayActivity(6215):onResume执行...
//屏幕熄灭
08-04 17:24:28.943:ERROR / PlayActivity(6215):在onPause执行...
08-04 17:24:32.113:ERROR / PlayActivity(6215):的onStop执行...
08-04 17:24:32.113:ERROR / PlayActivity(6215):执行的onDestroy ...
08-04 17:24:32.983:ERROR / PlayActivity(6215):ONSTART执行...
08-04 17:24:32.983:ERROR / PlayActivity(6215):onResume执行...
08-04 17:24:32.983:ERROR / PlayActivity(6215):在onPause执行...
//屏幕的推移
08-04 17:24:47.683:ERROR / PlayActivity(6215):onResume执行...
//锁拆除
08-04 17:24:56.943:ERROR / PlayActivity(6215):在onPause执行...
08-04 17:24:59.663:ERROR / PlayActivity(6215):的onStop执行...
08-04 17:24:59.663:ERROR / PlayActivity(6215):执行的onDestroy ...
08-04 17:25:00.943:ERROR / PlayActivity(6215):ONSTART执行...
08-04 17:25:00.943:ERROR / PlayActivity(6215):onResume执行...
 

我完全糊涂了。为什么要重新开始活动时,屏幕熄灭?为什么要停,并再次重新启动它时,屏幕已经上仅去除锁?

要确保我没有做错什么,我创建了一个新的项目,仅此活动。输出是相同的...

 公共类LifeCycleTest延伸活动{

    私人最终静态字符串DEBUG_TAG =FirstLifeLog;

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.e(DEBUG_TAG的onCreate执行......);
        的setContentView(R.layout.main);
    }

    保护无效onRestart(){
        super.onRestart();
        Log.e(DEBUG_TAG,onRestart执行......);
    }

    保护无效的OnStart(){
        super.onStart();
        Log.e(DEBUG_TAG,ONSTART执行......);
    }

    保护无效onResume(){
        super.onResume();
        Log.e(DEBUG_TAG,onResume执行......);
    }

    保护无效的onPause(){
        super.onPause();
        Log.e(DEBUG_TAG,在onPause执行......);
    }

    保护无效的onStop(){
        super.onStop();
        Log.e(DEBUG_TAG的onStop执行......);
    }

    保护无效的onDestroy(){
        super.onDestroy();
        Log.e(DEBUG_TAG的onDestroy执行......);
    }
}
 

是否有人有一个想法?

这是今天更新(不明白为什么它的行为方式不是像上次那样,也许更多的免费资源?)

  //活动开始
十二月8号至9号:14:03.122:ERROR / FirstLifeLog(15406)的onCreate执行...
十二月8号至9号:14:03.132:ERROR / FirstLifeLog(15406):ONSTART执行...
十二月8号至9号:14:03.132:ERROR / FirstLifeLog(15406):onResume执行...
//屏幕关闭
十二月8号至9号:14:07.412:ERROR / FirstLifeLog(15406):在onPause执行...
//在屏幕上
十二月8号至9号:14:11.722:ERROR / FirstLifeLog(15406):onResume执行...
//没有日志删除锁屏
 

解决方案

请参阅活动生命周期的文档生命周期的一个很好的说明,用图。

最有可能你的活动被杀害与屏幕熄灭,以节约资源(电池电量)。作为文档状态,你基本上可以随时杀死的机器人要释放资源。所以,你应该总是设计你的活动能够停止和重新启动在任何时间。

Information:My device is a Nexus One with 2.2 and I have tested two projects, one on 1.5 and one on 2.1.

Problem:I have trouble to understand the life cycle of my application when the screen is turned off and on.

Here is my output

// activity starts
08-04 17:24:17.643: ERROR/PlayActivity(6215): onStart executes ...
08-04 17:24:17.643: ERROR/PlayActivity(6215): onResume executes ...
// screen goes off
08-04 17:24:28.943: ERROR/PlayActivity(6215): onPause executes ...
08-04 17:24:32.113: ERROR/PlayActivity(6215): onStop executes ...
08-04 17:24:32.113: ERROR/PlayActivity(6215): onDestroy executes ...
08-04 17:24:32.983: ERROR/PlayActivity(6215): onStart executes ...
08-04 17:24:32.983: ERROR/PlayActivity(6215): onResume executes ...
08-04 17:24:32.983: ERROR/PlayActivity(6215): onPause executes ...
// screen goes on
08-04 17:24:47.683: ERROR/PlayActivity(6215): onResume executes ...
// lock removed
08-04 17:24:56.943: ERROR/PlayActivity(6215): onPause executes ...
08-04 17:24:59.663: ERROR/PlayActivity(6215): onStop executes ...
08-04 17:24:59.663: ERROR/PlayActivity(6215): onDestroy executes ...
08-04 17:25:00.943: ERROR/PlayActivity(6215): onStart executes ...
08-04 17:25:00.943: ERROR/PlayActivity(6215): onResume executes ...

I am totally confused. Why restarting the activity when the screen goes off? And why stop and restarting it again when the screen was already on and only the lock was removed?

To make sure I haven't done anything wrong, I created a new project with only this activity. The output is identically...

public class LifeCycleTest extends Activity {

    private final static String DEBUG_TAG = "FirstLifeLog";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(DEBUG_TAG, "onCreate executes ...");
        setContentView(R.layout.main);
    }

    protected void onRestart() {
        super.onRestart();
        Log.e(DEBUG_TAG, "onRestart executes ...");
    }

    protected void onStart() {
        super.onStart();
        Log.e(DEBUG_TAG, "onStart executes ...");
    }

    protected void onResume() {
        super.onResume();
        Log.e(DEBUG_TAG, "onResume executes ...");
    }

    protected void onPause() {
        super.onPause();
        Log.e(DEBUG_TAG, "onPause executes ...");
    }

    protected void onStop() {
        super.onStop();
        Log.e(DEBUG_TAG, "onStop executes ...");
    }

    protected void onDestroy() {
        super.onDestroy();
        Log.e(DEBUG_TAG, "onDestroy executes ...");
    }
}

Does someone have an idea?

Update from today (dont understand why it behaves not like last time, maybe more free resources?)

// activity starts
08-09 12:14:03.122: ERROR/FirstLifeLog(15406): onCreate executes ...
08-09 12:14:03.132: ERROR/FirstLifeLog(15406): onStart executes ...
08-09 12:14:03.132: ERROR/FirstLifeLog(15406): onResume executes ...
// screen off
08-09 12:14:07.412: ERROR/FirstLifeLog(15406): onPause executes ...
// screen on
08-09 12:14:11.722: ERROR/FirstLifeLog(15406): onResume executes ...
// no log for removed screen lock
解决方案

See Activity Lifecycle documentation for a good description of the lifecycle, with diagrams.

Most likely your activity is killed with the screen goes off to save resources (battery power). As the documentation states, you can basically be killed anytime that Android wants to free resources. So, you should always design your activities to be able to be stopped and restarted at any time.

这篇关于问题的理解生命周期时,屏幕熄灭,在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 11:17