我是新来的android开发,现在我想了解android活动的生命周期。onPause方法没有被调用
我有这些方法对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()永远不会被调用。我尝试按Home键,从最近的应用程序列表中选择其他应用程序,关闭屏幕显示,但它只是直接调用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被调用?
编辑:你可以告诉我onStop()是否在onPause()之后调用什么时候调用onResume()?是不是onResume支持恢复由onPause()暂停?在我的测试程序中,onResume()仅在onStart()之后在onPause()之后调用。可能是因为我只有一项活动?
你不重写'Activity.onPause'。 – adneal
该代码不显示任何'onPause()'方法的日志记录。你怎么知道它没有被调用?相信我......它会被调用。 – Squonk
感谢大家回答我愚蠢的问题。我无法相信我没有注意到这一点。 :( –