1

When i first start an activity from my main activity, it goes through the onCreate() then onStart(). While in the app, if i turn off the screen, it goes to onPause() where i save some data. Then immediately goes to onStop(), then onDestroy(), then again, without turning on the screen to onCreate(), onStart(), onPause(). Should it go to onCreate(), onStart() right after onDestroy(), without me doing anything?

@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();
}
Nicos
  • 303
  • 2
  • 19

2 Answers2

4

Shorter Explanation(Considering you are not using fragments):

  • Creation of activity is OnCreate,OnStart,OnResume
  • When you rotate the screen OnPause,OnStop,OnDestroy- Again OnCreate,OnStart,OnResumebecause activity is destroyed and recreated onOrientation change

Thus, if you won't rotate OnCreate,OnStart,OnResume is not fired for the second time !


Understand the Which events are executed and when:

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


A Use case Example(Considering you are using fragments):

Sample Project:

I have an opensource project Download it here and execute it and see the log to understand >the sequence of events executes one after another(I have fragment also in this project exclude the >fragment logs if you don't need it)

Project Observations

  • If the path is Activity-FragmentOne and change the orientation for the first time then the events fired are as follows MainActivity-onCreate- FragmentOne-onAttach- FragmentOne-onCreate- FragmentOne-onCreateView- FragmentOne-onActivityCreated- FragmentOne-onStart- FragmentOne-onResume

  • If the path is Activity-FragmentOne-orientationchange and change the orientation for the first time then the events fired are as follows

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

  • If the path is Activity-FragmentOne-orientationchange-orientationchange and change the orientation for the first time then the events fired are as follows

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

  • If the path is Activity-FragmentOne-orientationchange-orientationchange-FragmentTwo and change the orientation for the first time then the events fired are as follows

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

  • If the path is Activity-FragmentOne-orientationchange-orientationchange-FragmentTwo-orientationchange and change the orientation for the first time then the events fired are as follows

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


Community
  • 1
  • 1
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • Unless I am missing something, this answer does not give a correct answer to this question the OP has asked: 'Should it go to onCreate(), onStart() right after onDestroy(), without me doing anything?' According to your answer, onCreate() and onStart() will not be called in case there is no rotation. So if the OP really does not rotate, then calls to onCreate() and onStart() would not be observed. – dragi Oct 17 '14 at 12:14
  • 1
    Actually when the screen is turned off, it goes to portrait from landscape, and then when you turn it on vice versa. So he is right, at least it worked for me. – Nicos Oct 18 '14 at 20:34
0

I have observed the same behaviour on a tablet, with only landscape mode. It turns out that when the screen goes off, it is considered portrait mode. That's why during screen lock the activity is once destroyed for the screen off itself and then it goes all the way into portrait mode and it is again destroyed.
I have read this here on SO, but I cannot recall where it was, to link against it.

dragi
  • 3,385
  • 5
  • 22
  • 40