How to avoid activity re-creation when being rotat

2019-02-28 06:03发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-28 06:28

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

android:screenOrientation="user"

Together with the other configChanges values, this works perfectly.

查看更多
登录 后发表回答