无法理解屏幕关闭和打开时的生命周期

发布于 2024-09-12 16:19:15 字数 2807 浏览 1 评论 0原文

信息: 我的设备是 2.2 版本的 Nexus One,我测试了两个项目,一个在 1.5 上,一个在 2.1 上。

问题: 当屏幕关闭和打开时,我很难理解应用程序的生命周期。

这是我的输出,

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

我完全困惑了。为什么屏幕关闭后要重新启动 Activity?当屏幕已经打开并且只有锁被移除时,为什么要停止并重新启动它呢?

为了确保我没有做错任何事情,我创建了一个仅包含此活动的新项目。输出是相同的...

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 ...");
    }
}

有人有想法吗?

从今天开始更新(不明白为什么它的表现不像上次,也许更多的免费资源?)

// 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪意 2024-09-19 16:19:15

我自己的游戏也有同样的问题。我的游戏仅在横向模式下运行,当关闭屏幕时,Android 屏幕保护程序会接管控制权(在纵向模式下),从而发送一个orientationChange来破坏并重新创建活动。

一个简单的解决方案是声明您将管理自己的屏幕方向更改:

<activity ... android:configChanges="orientation" ... >

如果您的 Activity 被声明为仅横向(您无需执行任何操作),则这很容易,但如果您的 Activity 可以旋转,则可能会变得更困难...

I had the same issue with my own game. My game works in landscape only, and when turning off the screen, the android screensaver takes the control (in portrait mode), thus sending an orientationChange that destroys and recreates the activity.

A simple solution is to declare that you will manage yourself screen orientation changes:

<activity ... android:configChanges="orientation" ... >

This is quite easy if your activity is declared to be landscape only (you have to do nothing), but can get harder if your activity can rotate...

安人多梦 2024-09-19 16:19:15

Ruben 的答案完全正确,但前提是您的应用程序面向 API 级别 12 或更低

但从 API 级别 13 开始,除了 orientation 选项之外,您还必须声明 screenSize 选项,因为当设备在纵向和横向之间切换时也会触发该选项方向:

<activity ... android:configChanges="orientation|screenSize" ... >

否则,当 API 13 或更高版本的平台上屏幕关闭时,您的 Activity 仍会再次重新创建。

有关参考,请参阅 API 文档android: configChanges 部分注释。

Ruben's answer is completely correct, but only if your application targets the API level 12 or lower.

But since the API level 13 in addition to the orientation option, you have to declare the screenSize option, because it also gets triggered when a device switches between the portrait and the landscape orientations:

<activity ... android:configChanges="orientation|screenSize" ... >

Otherwise, your activity would still be recreated an additional time when screen goes off on the API 13 or a higher platform.

For reference, see API docs, android:configChanges section notes.

萌化 2024-09-19 16:19:15

就是这样。如果您阅读活动生命周期,您会发现步骤几乎都是这样排序的。这不仅是当你的屏幕打开和关闭时,也是当你改变手机的方向时。 Android 完全按照您上面提到的步骤重新创建了 Activity。尝试旋转屏幕,你就会看到! =)

Thats the way. If you read the activity life cycle you will see that the steps are pretty much ordered that way. Its not just when your screen goes on and off but also when you chnage the oreintation of the phone. Android recreated the activity following exactly the steps you have mentioned above. Try rotating you screen, you will see then! =)

攒一口袋星星 2024-09-19 16:19:15

请参阅 Activity 生命周期 文档,了解生命周期的详细描述以及图表。

您的活动很可能会因屏幕关闭而被终止,以节省资源(电池电量)。正如文档所述,当 Android 想要释放资源时,你基本上可以随时被杀死。因此,您应该始终将活动设计为能够随时停止和重新启动。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文