信息:我的设备是带有 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