Android Question Preventing Activity_Create code from executing when screen orientation has changed

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

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.
 

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

Thanks for the quick reply.

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.
 
Upvote 0

TomA

Active Member
Licensed User
Longtime User
Hi Everyone,
Can you tell me how to prevent Activity_Create code from executing when the user changes the screen orientation?
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.

If you haven't already read it, check out http://www.b4x.com/android/forum/threads/android-process-and-activities-life-cycle.6487/ for the Android process and activities life cycle.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Thanks for all the replies.

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
 
Last edited:
Upvote 0
Top