屏幕熄灭时了解生命周期的问题

War*_*ith 21 lifecycle android screen android-activity

信息:我的设备是带有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 ...
Run Code Online (Sandbox Code Playgroud)

我完全糊涂了.为什么在屏幕关闭时重新启动活动?为什么当屏幕已经打开并且仅删除锁定时再次停止并重新启动它?

为了确保我没有做错任何事,我创建了一个只有这个活动的新项目.输出相同......

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 ...");
    }
}
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

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

// 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
Run Code Online (Sandbox Code Playgroud)

Rub*_*ben 30

我对自己的游戏也有同样的问题.我的游戏仅在横向上工作,当关闭屏幕时,Android屏幕保护程序采用控制(在纵向模式下),从而发送direChange,用于销毁和重新创建活动.

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

<activity ... android:configChanges="orientation" ... >
Run Code Online (Sandbox Code Playgroud)

如果您的活动被宣布为仅景观(您必须不做任何事情),这很容易,但如果您的活动可以轮换,则会变得更难...

  • 这可能应该被设计为已接受的答案,因为尽管它在一个月之后出现,但它确定了另一个中留下神秘的原因. (3认同)

Ale*_*mov 5

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

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

<activity ... android:configChanges="orientation|screenSize" ... >
Run Code Online (Sandbox Code Playgroud)

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

如需参考,请参阅API 文档android:configChanges章节注释。


Che*_*mon -2

请参阅活动生命周期文档,以获取生命周期的详细描述和图表。

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