简体   繁体   中英

Need orientation locked to landscape, but still need notification when rotated to portrait

I am working on an Android app that requires the screen be locked to landscape orientation since the UI is designed for landscape only. However, what's happening on the screen still needs to know when the device has been rotated to portrait mode. If I put the following in AndroidManifest.xml:

android:screenOrientation="landscape"

This prevents screen orientation change notifications (onConfigurationChanged) from being fired. But, if I set the screenOrientation to "sensor," the OS automatically rotates the UI without a way to prevent it:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    //Can't override newConfig and force landscape? wtf?
    super.onConfigurationChanged(newConfig);
}

Am I just approaching this the wrong way? How can I keep the screen landscape (preventing the OS from automatically changing the orientation) but still get notifications when the orientation is changed?

I think what you want is:

<activity ...
    android:configChanges="orientation"
    ...

According to documentation for Android manifest <activity> element:

android:configChanges

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

...

Value: "orientation"

Description: The screen orientation has changed — the user has rotated the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM