Normally rotating device by 90 degrees, (portrait to landscape, or reverse) causes a configuration change, Activity is destroyed and re-created etc, so it can just save value of Display.getRotation()
at start and work with it.
However, when rotating device directly from 0 to 180 (portrait to portrait), or 90 to 270(landscape to landscape), no configuration change is done, device simply re-maps the screen. This makes sense since aspect ratio of layout is not changed and it need not be altered. But this makes it impossible for Activity to detect when such changes happen, i.e when Surface.ROTATION_90
went to Surface.ROTATION_270
etc.
Apart from polling Display.getRotation()
, is there any better way to detect this change ?
You can use the helper class OrientationEventListener.
Something like this:
public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN) return;
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: ....
case Surface.ROTATION_90: ....
case Surface.ROTATION_180: .....
case Surface.ROTATION_270: .....
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With