奇怪的活动生命周期。

时间:2014-10-15 11:37:28

标签: android

当我第一次从我的主要活动开始活动时,它会通过onCreate()然后onStart()。在应用程序中,如果我关闭屏幕,它会转到onPause(),我保存一些数据。然后立即转到onStop(),然后转到onDestroy(),然后再转到onCreate(),onStart(),onPause()。 它应该在onDestroy()之后立即进入onCreate(),onStart(),而我没有做任何事情吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_timegame);

}

@Override
protected void onResume() {
    super.onStart();
    if (checkFirstTimeRun())
        initializeGame();
    else
        resumeGame();
}

@Override
protected void onPause() {
    super.onPause();
    saveGameData();
}

2 个答案:

答案 0 :(得分:3)

更短的说明(Considering you are not using fragments

  • 活动的创建时间为OnCreateOnStartOnResume
  • 当您旋转屏幕OnPauseOnStopOnDestroy时 - 再次 OnCreateOnStartOnResume因为活动被销毁并在onOrientation change上重新创建

因此,如果您不能轮换OnCreateOnStartOnResume第二次不会被解雇!


了解执行哪些事件以及何时执行:

<强> Have a look at one of my answers i posted in stackoverflow - Click Here


用例示例(Considering you are using fragments):

示例项目

  

我有一个开源项目Download it here并执行它并查看要了解的日志&gt;事件序列一个接一个地执行(我在这个项目中也有片段排除&gt;片段日志if你不需要它

项目观察

  • 如果路径为Activity - FragmentOne并且第一次更改方向,则触发的事件如下 MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume

  • 如果路径为Activity - FragmentOne - orientationchange并首次更改方向,则触发的事件如下:

MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume - FragmentOne-onPause - FragmentOne-onSaveInstanceState - FragmentOne-onStop - FragmentOne-onDestroy - FragmentOne-onDetach - MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume

  • 如果路径为Activity - FragmentOne - orientationchange - orientationchange并首次更改方向,则触发的事件如下:

MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume - FragmentOne-onPause - FragmentOne-onSaveInstanceState - FragmentOne-onStop - FragmentOne-onDestroy - FragmentOne-onDetach - MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume - FragmentOne-onPause - FragmentOne-onSaveInstanceState - FragmentOne-onStop - FragmentOne-onDestroy - FragmentOne-onDetach - MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume

  • 如果路径为Activity - FragmentOne - orientationchange - orientationchange - FragmentTwo并首次更改方向,则触发的事件为关注

MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume - FragmentOne-onPause - FragmentOne-onSaveInstanceState - FragmentOne-onStop - FragmentOne-onDestroy - FragmentOne-onDetach - MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume - FragmentOne-onPause - FragmentOne-onSaveInstanceState - FragmentOne-onStop - FragmentOne-onDestroy - FragmentOne-onDetach - MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume - FragmentOne-onStop - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentTwo-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume

  • 如果路径为Activity - FragmentOne - orientationchange - orientationchange - FragmentTwo - orientationchange并首次更改方向然后发起的事件如下

MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume - FragmentOne-onPause - FragmentOne-onSaveInstanceState - FragmentOne-onStop - FragmentOne-onDestroy - FragmentOne-onDetach - MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume - FragmentOne-onPause - FragmentOne-onSaveInstanceState - FragmentOne-onStop - FragmentOne-onDestroy - FragmentOne-onDetach - MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentOne-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume - FragmentOne-onStop - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentTwo-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume - FragmentOne-onPause - FragmentOne-onSaveInstanceState - FragmentOne-onSaveInstanceState - FragmentOne-onStop - FragmentOne-onDestroy - FragmentOne-onDetach - FragmentOne-onDestroy - FragmentOne-onDetach - MainActivity-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onAttach - FragmentOne-onCreate - FragmentOne-onCreateView - FragmentTwo-onActivityCreated - FragmentOne-onStart - FragmentOne-onResume


答案 1 :(得分:0)

我在平板电脑上观察到相同的行为,只有横向模式。 事实证明,当屏幕熄灭时,它被认为是肖像模式。这就是为什么在屏幕锁定期间,活动一旦被屏幕关闭而被破坏,然后它一直进入纵向模式并再次被破坏。
我已经在这里读到了这个,但是我不记得它在哪里,与之相关联。