【发布时间】:2011-03-25 08:18:07
【问题描述】:
信息: 我的设备是带有 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 ...
我完全糊涂了。为什么在屏幕熄灭时重新启动活动?当屏幕已经打开并且只有锁定被移除时,为什么要停止并重新启动它?
为了确保我没有做错任何事,我创建了一个仅包含此活动的新项目。输出相同...
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
【问题讨论】:
-
奇怪的是我看到 onDestroy()、onStart()、onResume() 调用,但我没有看到任何 onCreate() 调用。我也有兴趣了解这种行为。
-
那是因为我的 onCreate 错过了日志语句......我今天又试了......现在我的游戏有相同的行为,但 testproject 有预期的行为(请参阅更新)跨度>
标签: android android-activity screen lifecycle