Reputation: 32271
I want to make possible to use an activity in landscape / portrait. The activity starts with the layout for the current orientation (that means before starting the activity). After that it has to stick to it, and not react to orientation change.
I tried putting
android:configChanges="orientation"
in the manifest of the activity, and overriding
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
to do nothing (tried commenting the super call too, but this lead to exception) but this has not the effect disabling the rotation change - the change is processed and the layout reconstructed, it just doesn't use the correct one.
And I can't use
android:screenOrientation
Because it seems I have to specify only one mode for always, and that's not what I need either. And anyways, if I specify something there, the activity gets reconstructed when rotating. Tried with
android:screenOrientation="nosensor"
that doesn't do anything
Here there's a lock of current orientation with code http://www.samcoles.co.uk/mobile/android-lock-and-unlock-screen-orientation/
But it achieves the same effect as specifying orientation in XML (keeps layout but reconstructs the activity). It's a bit nearer to what I want (keeps orientation from start), but reconstructs the activity and I don't want it to react at all.
Upvotes: 1
Views: 1134
Reputation: 171
Add this to onCreate
if (this.getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}
else {setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}
Upvotes: 0
Reputation: 6131
Try
@Override
public void onConfigurationChanged(Configuration newConfig) {
newConfig.orientation = getResources().getConfiguration().orientation;
super.onConfigurationChanged(newConfig);
}
Upvotes: 0
Reputation: 8695
android:configChanges="orientation" doesn't work on the emulator at all, but it works fine on devices.
Upvotes: 1