Home > Blockchain >  The lifecycles triggered by screen orientation change are different on an Android emulator and a rea
The lifecycles triggered by screen orientation change are different on an Android emulator and a rea

Time:01-23

I observed the set of activity lifecycle callbacks triggered after screen orientation change are different when running an app on an emulator (running Android 10) and a real Android device (a Pixel XL running Android 10).

Suppose I just create a simple app with only one activity. In the activity, I override two lifecycle callbacks to print out messages indicating the callbacks are triggered. And I did not set android:configChanges.

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

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

Then I run the app on an emulator and a Pixel XL device (both running Android 10) but I observe different outputs.

On the Android emulator, nothing is printed out after screen rotation.

On the Pixel XL device, "onCreate" and "onResume" are outputed after screen rotation.

Obviously the behavior on the real device is correct because it is consistent with the Android doc.

I use the latest version of Android Studio (Arctic Fox | 2020.3.1 Patch 4). I can reproduce this issue on two emulator versions (31.1.4-7920983 and 30.0.5.0-6306047). I can also reproduce it on Android 10 and 11, but not on Android 7.

Is it a bug on the Android emulator or the system images running on it?

Does anyone else observe similar behaviors? If not, can you share me with your emulator version and System image version?

CodePudding user response:

Have you tried using Logcat? For example:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("TEST", "onCreate");
}

protected void onResume() {
        super.onResume();
        Log.d("TEST", "onResume");
}

For more information, read this.

CodePudding user response:

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.

  •  Tags:  
  • Related