Preventing the screen from changing orientation

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Preventing the screen from changing orientation.

Can Basic4Android do this?

If it can, please show the coding I need to use for it.

I plan to lock orientation changing on some activities such as those that use edit boxes with a database to stop the need for extra coding that has to reload data if the user changes the phone orientation.

Thanks.
 

corwin42

Expert
Licensed User
Longtime User
Reload data on screen orientation change? For why??

Because the user interface gets completely recreated and all currently displayed data is lost.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Thanks for the replies.

I experimented a bit and took the suggestion from eps. I put a variable in Process_Globals and did this in the following subs and it works in both orientations. I don't loose anything:

B4X:
Sub Process_Globals
   Dim SQL As SQL
   Dim strResultDescription As String
End Sub

Sub Activity_Resume
   EditTextResultDescription.Text = strResultDescription
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   strResultDescription = EditTextResultDescription.Text
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It is still possible to lose your data if the user starts another app or goes to the home screen while your app is running. Your application goes into the background, where it can be killed by the OS if it is running out of space.

If this would be an issue for you, you need to write the data which you now have in Process_Globals to disk in activity_Pause and read it again on activity_Resume. Or use the State_Manager module
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Steve,

It should not be an issue, at least for now because I also tested it using the Home button and the Back button and found the text was still showing in the text box when I returned to that screen.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It depends more on if you are going to distribute the app. I have several devices with differing amounts of available memory. On some the apps in the background never seem to get killed, on others the apps nearly always get killed.

If you are giving or selling your app to other users, you need to be prepared for this eventuality. Especially if it's important data.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
You might want to look at these Wiki pages if you haven't done so already.

For one thing, you have:

B4X:
Sub Activity_Resume
   EditTextResultDescription.Text = strResultDescription
End Sub

The link above explains why Activity_Resume is not the correct place for that code.

Another thing is that you said that you are not losing data when you press the Back key and return. While it may be possible that this happens on your device, I've never heard of one which acts like this and even if yours does, most don't -- they WILL lose the data.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

Thanks for the wiki link. I'll read that one. :)


You might want to look at these Wiki pages if you haven't done so already.

For one thing, you have:

B4X:
Sub Activity_Resume
   EditTextResultDescription.Text = strResultDescription
End Sub

The link above explains why Activity_Resume is not the correct place for that code.

Another thing is that you said that you are not losing data when you press the Back key and return. While it may be possible that this happens on your device, I've never heard of one which acts like this and even if yours does, most don't -- they WILL lose the data.
 
Upvote 0

keeponsmurfin

New Member
Licensed User
Longtime User
Go to B4A Project menu - Orientations Supported option, and select whether you want to lock orientation to portrait or landscape. This will lock orientation for your project.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Thanks keeponsmurfin for the reply. :)

I like your name. I remember that tv show "The Smurfs" and now my children aged 7 & 9 watch it on YouTube.
 
Upvote 0
Top