How to avoid activity re-creation when being rotat

2019-02-28 05:53发布

问题:

Background

Each time the device is being rotated, Android will destroy the current activity and re-create it with the new configuration.

An easy way to avoid activity re-creation is by adding "configChanges" values to the manifest ("screenSize" and "orientation" as I recall).

The problem

If you use the "configChanges" attribute, the app won't respect the orientation lock feature that some (or all?) Android devices have, which prevent anything from being rotated when the screen is being rotated.

What I've tried

I tried playing with the "screenOrientation" attribute values, yet none of them achieved what I wanted.

The question

How can I prevent the activity from being re-created when the screen changes its orientation, while also respecting the user preference of orientation-lock?

Is it perhaps possible to get the value of this preference, so that I could let the activity lock its orientation when it's being set?

回答1:

OK, never mind. I think I just missed the correct one:

android:screenOrientation="user"

Together with the other configChanges values, this works perfectly.