Android Code Snippet Prevent (unwanted) Screen Rotation

StateManager will save the vars (and restore) when screen flips from Portrait to Landscape (and back).
However, as I have found, when a custom panel is in view, and screen rotates, it is dismissed and you have to start over (unless you like to create MUCH code to work around this).

So, to avoid all this, I lock the screen to the orientation (either port or land) when the user first accessed the selected activity. This prevents the screen from flipping during the activity session.
IF this activity DOES NOT call another activity, you may not require StateManager...

In the Activity_Resume, Include... yourcodemod.ForceOro

In the Activty_Pause include... yourcodemod.ReSetOro

The above subs are placed in your code module.


B4X:
Sub ForceOro
Dim loa As LayoutValues
   
Dim pl As Int = -1
'  pl = Port or Land   - 1 is any, 0 = landscape, 1 = portrait

If loa.Height > loa.Width Then
     pl = 1
Else
     pl = 0
End If

ph.SetScreenOrientation( pl ) ' requires phone lib in your code module...

End Sub


B4X:
Sub ReSetOro
    ph.SetScreenOrientation( - 1 ) ' set to any rotation
End Sub


In my mind, it is a pity we have to do any of this. Rotating the screen is handled badly in Android. At the very least, Android should have had a setting to Lock the activity to current Oro (Resume), and set it either when Activity.Finish (or Pause). Look at the hoops (and code) we must create to deal with this... time after time...
 
Top