Can you tell me how to prevent Activity_Create code from executing when the user changes the screen orientation?
I have several layouts. The main layout is loaded in Activity_Create. Additional layouts are loaded based on button clicks and I want to keep the current layout displayed and not have the main layout load up each time the screen changes orientation.
You cannot prevent the execution of code, you will have to somehow save the info you need, Activity_Create will always execute when you rotate the device.
I will see if I can figure out a way to do it with a flag. I will upload the code once I find something that works in case others have the same issue as me.
Activity_Create is called with the boolean FirstTime set to true only the first time it executes. FirstTime is false for subsequent exections. So you need to save anything you will need on subsequent calls to keep the items the same. All the layouts are recreated each time but the current contents should be saved as they change so each time it is called again, you will have the info needed to restore the state. So you end up with some of the code contained in a 'If FirstTime then (stuff to do only the first time) else (stuff to do only if FirstTime is false) end if' conditional statement. Stuff to be done every time Activity_Create is called can be outside of this conditional statement. This is pretty standard in programming for Android.
Here's how I was able to do it. I set the name of the Activity to keep on the screen in a variable and loaded that one in the Activity_Create sub routine since that one is called when the screen is rotated.
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim strNameOfCurrentActivity As String : strNameOfCurrentActivity = "main"
End Sub
B4X:
Sub Activity_Create(FirstTime As Boolean)
Select strNameOfCurrentActivity
Case "main"
Activity.LoadLayout("main")
Activity.SetBackgroundImage(LoadBitmap(File.DirAssets, "IMG_0270.jpg"))
Case "settings"
ButtonSettings_Click
End Select
End Sub
B4X:
Sub ButtonSettings_Click
strNameOfCurrentActivity = "settings"
Activity.LoadLayout("settings")
PanelSettings.SetBackgroundImage(LoadBitmap(File.DirAssets, "IMG_0327.jpg"))
' Make the panel the same size as the activity.
'----------------------------------------------
PanelSettings.Width = Activity.Width
PanelSettings.Height = Activity.Height
End Sub