当我第一次从主要活动开始活动时,它先经过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();
}

最佳答案

简短说明(Considering you are not using fragments):


活动的创建为OnCreateOnStartOnResume
旋转屏幕OnPauseOnStopOnDestroy-再次
OnCreateOnStartOnResume,因为在方向更改时活动被销毁并重新创建


因此,如果您不旋转OnCreateOnStartOnResume,则不会第二次触发!



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

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



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

示例项目:


  我有一个开放源代码项目Download it here并执行它,然后查看日志以了解>事件序列是一个接一个地执行的(我在项目中也有片段,如果不需要的话,请>片段日志除外)


项目观察


如果路径是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

09-05 19:02