【发布时间】:2014-06-02 16:40:00
【问题描述】:
我是 android 开发新手,现在我正在尝试了解 android 活动的生命周期。
我有这些方法可以在logcat上打印出生命周期过程:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, "In the onCreate() event");
}
public void onStart() {
super.onStart();
Log.d(tag, "In the onStart() event");
}
public void onRestart() {
super.onRestart();
Log.d(tag, "In the onRestart() event");
}
public void onResume() {
super.onResume();
Log.d(tag, "In the onResume() event");
}
public void onStop() {
super.onStop();
Log.d(tag, "In the onStop() event");
}
public void onDestroy() {
super.onDestroy();
Log.d(tag, "In the onDestroy() event");
}
问题是永远不会调用 onPause()。我尝试按主页按钮,从最近的应用程序列表中选择其他应用程序,关闭屏幕显示,但它只是直接调用 onStop() 而不是调用 onPause() 然后调用 onStop()。我正在阅读的书说,如果将活动推入后台,则支持 onPause() 后跟 onStop()。
书籍内容:
Click the Phone button on the Android emulator so that the activity is pushed to the background.
Observe the output in the LogCat window:
11-16 06:32:00.585: D/Lifecycle(559): In the onPause() event
11-16 06:32:05.015: D/Lifecycle(559): In the onStop() event
我正在运行 4.2.2 的真实设备和运行 2.3.3 的虚拟设备上进行测试,但结果是相同的。我只是误解了 onPause 的目的,还是我对要调用的 onPause 做错了?
编辑:你能告诉我是否在 onPause() 之后调用 onStop() 什么时候调用 onResume()? onResume 不支持 onPause() 暂停的恢复吗?在我的测试程序中,onResume() 仅在 onStart() 之后调用,而不是在 onPause() 之后调用。可能是因为我只有一项活动?
【问题讨论】:
-
你没有覆盖
Activity.onPause。 -
代码没有显示
onPause()方法的任何日志记录。你怎么知道它没有被调用?相信我......它会被调用。 -
感谢所有好心回答我愚蠢问题的人。我不敢相信我没有注意到这一点。 :(
-
每个人都会遇到:)