15

I have an activity that is showing a compass, and I need to know the orientation of the screen to properly rotate the needle. I need to distinguish between 90° and 270° degree rotation to properly handle this.

When I rotate the screen (on my Nexus S at least) the activity's onCreate gets called, e.g. when I rotate from portrait to landscape mode. When I rotate from one landscape mode to the other with the top edge raised, onCreate() gets called twice (once for portrait orientation and once for the target landscape mode).

However, when I rotate with the bottom edge up, the screen rotates 180° and onCreate is not getting called. Is there an event that gets fired in this case?

Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
Michael
  • 1,416
  • 1
  • 9
  • 21
  • [That answer on another question helped me much more.](https://stackoverflow.com/a/30394613/433718) – OneWorld Jan 11 '23 at 15:59

5 Answers5

11

You are correct that at a 180 degree rotation you will not get restarted or get a configuration change, because the configuration has not actually changed. (It is still in landscape or portrait, and everything else is the same.)

However if you are showing a compass, that must mean you are updating it from sensor events. Thus you can just call Display.getRotation() each time you get a sensor update to get the current rotation of the screen. In fact that you need is rotation of interpreting sensor events (or actually how your drawing of them will be modified when it gets to the screen), not just the orientation.

I found a relevant blog post that is well worth reading.

Be sure to read the SDK documentation.

Also check out the discussion about using Display.getRotation() to correctly remap sensor coordinates.

BryanH
  • 5,826
  • 3
  • 34
  • 47
hackbod
  • 90,665
  • 16
  • 140
  • 154
  • hackbot, thanks that pointed me into the right direction. I am checking the screen rotation now on every sensor event and update the sensor alignment whenever it changes. This works, but I think I am wiring unrelated things now: It may (in theory) happen that the screen orientation changes with no sensor event but as this is only a theoretical possibility I guess it is the best option for now. – Michael Jan 30 '11 at 20:53
6

The helper class OrientationEventListener makes it very easy to get call backs on rotation. I would suggest you give it a try.

monchote
  • 3,440
  • 2
  • 20
  • 20
0

As Monchote said, in order to make your needle rotate as expected on the compass, it's better to lock your UI to portrait or landscape, and change your needle angle according to the device rotation angle, which can be gained easily from OrientationListener.

In this way, no matter the auto rotation feature is opened or not by user, you can always get rotation angle change notification. Here's a good example for your reference.

Daniel Kao
  • 354
  • 2
  • 7
0

Just take a look at the SensorManager Documentation, there you can get the best examples and explanations of how to use the accelerometer to get the most precise informations. Instead of making the things just by detecting the phone orientation, with that API you can get the exactly rotation, just like used by racing games like Asphalt and Need For Speed.

Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
  • That's fine for games but it's not the canonical way to handle rotation on regular apps. Instead Android apps should subscribe to configuration changes (such as screen rotation) and act accordingly. – Julio Gorgé Jan 30 '11 at 17:39
  • Sorry for being a little confusing on my question, it was actually about the screen rotation event, not the sensor readouts. – Michael Jan 30 '11 at 20:46
-2

Have a look at the OnConfigurationChanged method to track down the orientation change and re-arrange your UI as needed.

See some sample code

Julio Gorgé
  • 10,056
  • 2
  • 45
  • 60
  • 2
    Julio, this method has the same flaw in my special case, it is not being called :-( – Michael Jan 30 '11 at 20:44
  • 1
    You would need to modify your Manifest if you want to be notified of configuration changes. Default behavior is to tear the application down entirely and rebuild it, which is why onCreate() is being called. In general, you want to leave it this way. Properly handling configuration changes is very difficult. – Edward Falk Feb 26 '11 at 22:00
  • 1
    Downvoted because OnConfigurationChanged is NOT called in the case described by the OP. – Pooks Nov 15 '13 at 05:13