Android Question Back button select case statement

anaylor01

Well-Known Member
Licensed User
Longtime User
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")
 

anaylor01

Well-Known Member
Licensed User
Longtime User
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
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Can you give me an example?

I don't know your app; however, something like:
B4X:
[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
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Are these layouts?
APPSTATE_GAMELOADED
APPSTATE_HOWTOPLAY
APPSTATE_HOWTOPLAYOK
APPSTATE_ANSWER

No, they are constants.

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

anaylor01

Well-Known Member
Licensed User
Longtime User
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?
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
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
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I forgot to put it in debug mode.So it runs the code as expected but it exits the app. See below.
** Activity (main) Pause, UserClosed = true **
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I forgot to put it in debug mode.So it runs the code as expected but it exits the app. See below.
** Activity (main) Pause, UserClosed = true **
"See below": do you really think this can help us to help you? :)

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
Are you referring to this part, right?
Case "Mainmenu"
Msgbox("Exitapp","exitapp")
ExitApplication

and? The Msgbox is shown but the app exits without waiting for your touch?


Anyway:

a) try your code on a real device;
b) insert a log to know what "layout" contains, before the Select statement
c)
Try to reproduce it in a small project and upload the project.
 
Last edited:
Upvote 0
Top