I want to create a select case statement for the back button. I guess my main problem is how to dectect which layout is loaded. So something like this.
Select case Backbutton
case Layout("Main") loaded
Activity.removelayout("Main")
Acitivty.Loadlayout("test")
case layout("Two")loaded
Activity.removelayout("Two")
Acitivty.Loadlayout("THree")
Here is the code I have but I think using button.IsInitialized isn't the right way to do it. I want to detect which layout is active and if certain ones are active than to go back to the previous layout and if the Main layout is active to exit the application.
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
Select True
Case btnLoadGame.IsInitialized = True
ExitApplication
Case btnHowToPlay.IsInitialized = True
Activity.RemoveAllViews
Activity.LoadLayout("lytNewGame")
Case btnHowToPlayOK.IsInitialized = True
Activity.RemoveAllViews
Activity.LoadLayout("lytOptions")
Case lblAnswer.IsInitialized = True
Activity.RemoveAllViews
Activity.LoadLayout("lytNewGame")
Case Else
Msgbox("notloaded","test")
End Select
End If
End Sub
[Process_Globals]
Private const APPSTATE_GAMELOADED As Int = 1
Private const APPSTATE_HOWTOPLAY As Int = 2
Private const APPSTATE_HOWTOPLAYOK As Int = 3
Private const APPSTATE_ANSWER As Int = 4 ' (OR APPSTATE_QUESTION)
Private AppState As Int
Then, for each "state" of your app, you will set AppState to one APPSTATE constant value and use it in the Select block in Activity_KeyPress:
B4X:
Select Case AppState
Case APPSTATE_GAMELOADED
Case APPSTATE_HOWTOPLAY
Case APPSTATE_HOWTOPLAYOK
Case APPSTATE_ANSWER
End Select
When your app change its state (for example, a question is shown to the user, you can store this "event" in a public variable (AppState in my example); then, you can check the AppState when/where you need to choose the action to take (for example, depending on the state you can choose the layout to load:
B4X:
Select Case AppState
Case APPSTATE_GAMELOADED
Activity.RemoveAllViews
Activity.LoadLayout("...")
Case APPSTATE_HOWTOPLAY
Activity.RemoveAllViews
Activity.LoadLayout("...")
Case APPSTATE_HOWTOPLAYOK
Activity.RemoveAllViews
Activity.LoadLayout("...")
Case APPSTATE_ANSWER
Activity.RemoveAllViews
Activity.LoadLayout("...")
End Select
You could use a unique Activity.RemoveAllView before the Select block, of course.
I think I understand. My new plan is this. Create ONE variable. When a layout is loaded assign the variable a value. Then use the select statement to tell which layout is loaded. Right?
I think I understand. My new plan is this. Create ONE variable. When a layout is loaded assign the variable a value. Then use the select statement to tell which layout is loaded. Right?
When I push the back button on the emulator it runs my msgbox line in the code but it exits the app. I put a breakpoint on the select statement but it doesn't break. Any ideas?
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
Select layout
Case "NewGame"
Msgbox("MainMenu","mainmenu")
MainMenu
Case "Options"
MainMenu
Case "GamePlay"
MainMenu
Case "LoadGame"
MainMenu
Case "Mainmenu"
Msgbox("Exitapp","exitapp")
ExitApplication
Case "HowtoPlay"
Options
Case Else
Msgbox("Need to create a backbutton for this","test")
End Select
End If
End Sub
When I push the back button on the emulator it runs my msgbox line in the code but it exits the app. I put a breakpoint on the select statement but it doesn't break