37

Which method of the lifecycle is called when orientation changes occur? My application executes the onResume() method or maybe reloads the whole activity because I've set one boolean to check whether it is first run or not. I've read onConfigurationChanged() starts when orientation change occur, is it true? How to handle this?

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • 1
    http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges – Marcin Gil Jun 11 '11 at 12:34
  • 1
    If i add android:configChanges="orientation", the whole activity will not be restarted, but onConfigurationChanged() will be called instead? – Nikola Despotoski Jun 11 '11 at 12:54
  • Remember that you can have *separate* layouts for portrait and landscape - then your activity needs to be recreated/reinflated. – Marcin Gil Jun 11 '11 at 13:49
  • @Marcin Gil I have a map and i have draw graphics on the users location, but when orientation occurs then the graphics is removed. I want to keep this. I have created reDraw() method which redraws the location, but the app restarts the whole activity... – Nikola Despotoski Jun 11 '11 at 14:48
  • See here: http://developer.android.com/resources/articles/faster-screen-orientation-change.html – Marcin Gil Jun 11 '11 at 19:43

2 Answers2

45

Interesting one...

Activity is start onResume() is which you declare in your XML by default.

And as I found from question answer on stack overflow is:

Orientation Change

  • onSaveInstanceState
  • onPause
  • onStop
  • onCreate
  • onStart
  • onRestoreInstanceState
  • onResume

Switch TO Activity 2

  • onSaveInstanceState
  • onPause

Orientation Change WHILE IN Activity 2

  • onStop
  • onCreate
  • onStart

Switchback BACK FROM Activity2

  • onResume

I'm guessing that because Activity 1 is hidden at the time of rotation, onRestoreInstanceState isn't called because there is no 'view' (i.e., it can't be seen/viewed). Also, it is entirely possible to have 2 completely different layout files for portrait/landscape which potentially have different UI elements with different IDs.

As a result, I'd say if you want to use the Bundle in onSaveInstanceState to save your own data, then you need to add extra logic in your onCreate (in Activity 1) to process your own data there (as well as doing it conditionally in onRestoreInstanceState).

In particular, you could maintain a 'last known' orientation field so that onCreate knows that it needs to process your own data because orientation has changed, rather than relying on onRestoreInstanceState being called.

gzinho
  • 123
  • 1
  • 9
Siten
  • 4,515
  • 9
  • 39
  • 64
  • 6
    Actually Orientation change = `onPause` -> `onSave...` -> `onStop` -> `onCreate` -> `onStart` -> `onResume` – Max May 20 '13 at 20:11
  • 1
    Yep, in all cases `onPause` goes first, then `onSaveInstanceState` – Max Ivashkevich Feb 12 '15 at 12:22
  • 2
    According to android documents, no one can anticipate whether onSaveInstanceState will be called before or after onPause. We can only say with surety that onSaveInstanceState is called after onStop http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle) – Abhinav Arora Jul 21 '15 at 08:00
  • 3
    ^^ that's not correct. We can only say that onSaveInstanceState is called BEFORE onStop -> "If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause()." – marko.petrovic Aug 05 '16 at 09:53
  • 6
    in my case `onDestroy` is also getting called on rotation – Vivek Mishra Oct 31 '16 at 13:12
  • 1
    Orientation change = onPause() -> onSaveInstanceState() -> onStop() -> onDestroy() -> onCreate() -> onStart() -> onRestoreInstanceState() -> onResume() – Shlomi Fresko Dec 17 '18 at 10:58
5
public class MainActivity extends AppCompatActivity {
private final static String TAG = "AppActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(Bundle) called");
    setContentView(R.layout.activity_main);
}

@Override
public void onStart() {
    super.onStart();
    Log.d(TAG, "onStart() called");
}

@Override
public void onPause() {
    super.onPause();
    Log.d(TAG, "onPause() called");
}

@Override
public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume() called");
}

@Override
public void onStop() {
    super.onStop();
    Log.d(TAG, "onStop() called");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy() called");
}

}

1) Try to run your app on your phone and/or emulator and open the Logcat => on top of the window select Verbose.

2) Now try to change the screen orientation (ex. from portrait => landscape mode).

I hope this alternative will give you more insight into the activity lifecycle.