1

Following the Android doc and some other stackoverflow posts, screen orientation change will make an activity restart (i.e., the activity will be destroyed and recreated). However, when I test it on different Android versions, the behaviors seems to be different.

For example, suppose I just print out "onCreate" and "onResume" at the beginning of onCreate() and onResume(), respectively.

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   System.out.println("onCreate");
}

@Override
protected void onResume() {
   super.onResume();
   System.out.println("onResume");
}

When I test rotation on Android emulators running Android 10 and 11, it outputs nothing after rotation.

But on Android 7, it outputs "onCreate" and "onResume" after rotation.

So it seems screen orientation change will not trigger any lifecycle callbacks on newer Android versions (BTW, I did not set android:configChanges)?

But I don't see any guidance mentioning this behavior change even on the Android documentation. Does anyone know where to find official documentations or specific code commits mentioning this change?

Richard Hu
  • 811
  • 5
  • 18
  • 2
    I just tested on Android 11 Real device. It outputs both `onCreate` & `onResume` & that is the intended behavior across all versions. Make sure that it's not an issue with emulators or logs not showing up in the studio. – Mayur Gajra Jan 22 '22 at 06:40
  • @MayurGajra Hi, thanks for your answer. I have updated to the newest Android studio. I deleted my old emulator and installed a new one (version 31.1.4). But it seems the behaviors does not change. – Richard Hu Jan 22 '22 at 08:17
  • @MayurGajra Hi, I also tested on a Pixel XL device running Android 10. It outputs both `onCreate` and `onResume`. It really seems to be an emulator bug or system image bug. I can reproduce this issue on two emulator versions (31.1.4-7920983 and 30.0.5.0-6306047). I am wondering can you reproduce the issue on an Android emulator? – Richard Hu Jan 22 '22 at 14:19
  • Does this answer your question? [The lifecycles triggered by screen orientation change are different on an Android emulator and a real device (both running Android 10)](https://stackoverflow.com/questions/70813684/the-lifecycles-triggered-by-screen-orientation-change-are-different-on-an-androi) – plplmax Jan 22 '22 at 22:57

1 Answers1

0

The reason is because starting from Android 9, users need to press a new "rotation" button in the navigation bar when screen rotation happens. If you do not press it, the activity will not be destroyed and recreated. Please see this doc for more details.

Richard Hu
  • 811
  • 5
  • 18