Disable orientation but detect orientation
I've got an activity. This activity works in portrait mode or landscape mode. In landscape mode, I load a new layout to show a graph. To detect the orientation, I set up this in my manifest :
android:screenOrientation="sensor" android:configChanges="orientation"
It works perfectly, because when I flip my phone, the new layout is loaded. But, in lanscape mode, the user can disable/enable the rotation with a button (it's a switcher).
I wrote this code :
public void onConfigurationChanged(Configuration pNewConfig) {
Log.v(DEBUG_TA开发者_运维百科G, "Current configuration changes");
if (pNewConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.v(DEBUG_TAG, "Screen orientation : LANDSCAPE");
if (pNewConfig.orientation != mCurrentOrientation) {
initTestLandscape();
mCurrentOrientation = pNewConfig.orientation;
}
} else {
Log.v(DEBUG_TAG, "Screen orientation : PORTRAIT");
if (!mIsLandscapeLocking
&& pNewConfig.orientation != mCurrentOrientation) {
initTestPortrait();
mCurrentOrientation = pNewConfig.orientation;
}
}
super.onConfigurationChanged(pNewConfig);
}
This code works, but when the user is in landscape mode and flips the phone to back in portrait mode and the lock is on, the lanscape layout flips in portrait mode and I don't want that!
I've added
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
but this code disables the onConfigurationChanged method and I can't detect if the user flips the phone again...
Can you help me?
make changes in android:configChanges:-
android:configChanges="orientation|screenSize|keyboard|keyboardHidden"
after changes you code should be work
精彩评论