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?
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.