Android Question [SOLVED] How to detect screen reorientations when Pause/Resume do not fire?

JackKirk

Well-Known Member
Licensed User
Longtime User
I've come across a situation which I need help with.

I have an app with #SupportedOrientations: unspecified

If the device is in landscape mode and I rotate the device about its horizontal axis (so that it stays in landscape mode) then at a roll of about 35deg the screen will flip - but Pause/Resume do not fire.

Similarly if the device is in portrait mode and I rotate the device about its vertical axis (so it stays in portrait mode) then at a pitch of about 35deg the screen will flip - again Pause/Resume do not fire.

Because Pause/Resume do not fire I have no absolute way of detecting the screen flip has happened.

I need to know when this happens because I am using a barcode reader library whose preview gets screwed up when the screen flips and I need to reinitialize the library to correct this.

I have played around with monitoring pitch and roll via Phone.PhoneOrientation and this works after a fashion but it is problematic in 2 ways:

(1) You have to pick a threshold pitch/roll at which to do the barcode library re-initialization - I have established purely by experimentation that 35-40 deg works on my Samsung S5 - but what if it is different on other devices?

(2) It is essential that the barcode library re-initialization occurs after the screen flips - meaning the threshold of (1) has to be correct otherwise you achieve nothing.

Any help would be appreciated...
 

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,

Nope - if you rotate the phone as I have indicated above so that the orientation does not change from either landscape or portrait then the screen flips without firing Pause/Resume.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Any chance you have set this attribute in the AndroidManifest?
android:configChanges="orientation|screenSize"

As far as I have understood, this will skip onCreate, Pause and Resume.

If you use this attribute and you still need to trigger the configuration change, you can use "onConfigurationChanged".
See this thread: https://www.b4x.com/android/forum/threads/onconfigurationchanged.64333/
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Hi,

Thanks for your response but no - I am not touching the manifest in any way.

I have attached a really simple project.

(1) Compile and run it.
(2) Turn the phone to landscape - log will show Pause/Resume fired.
(3) Rotate the phone about its long (i.e. horizontal axis) without it getting out of landscape mode.
(4) At some point the screen will flip - but Pause/Resume do not fire.

Am I in a real dusty little corner?
 

Attachments

  • Test.zip
    6.7 KB · Views: 139
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are correct. I misread your previous post. The activity will not be recreated if the change is from landscape to reverse landscape (or portrait to reverse portrait).

You can use this code to get the current orientation:
B4X:
Sub GetOrientation As Int
   Dim jo As JavaObject
   jo.InitializeContext
   Return jo.RunMethodJO("getSystemService", Array("window")).RunMethodJO("getDefaultDisplay", Null).RunMethod("getOrientation", Null)
End Sub

The possible values: http://developer.android.com/reference/android/view/Surface.html#ROTATION_0

No event will be raised. You can use a timer to check this every 100ms. It will be more efficient (and simpler) than monitoring the orientation sensor.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Thanks for that Erel, I was beginning to think my dusty little corner was also very lonely:)
 
Upvote 0
Top