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
Sorry for not explaining good enough. I am testing the back button right now. It runs through all the code correctly but it exits the app and in the log it says this "** Activity (main) Pause, UserClosed = true **". This is with the emulator and using the ESC key for the back button. I tested on a real device and does the same thing.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Sorry for not explaining good enough. I am testing the back button right now. It runs through all the code correctly but it exits the app and in the log it says this "** Activity (main) Pause, UserClosed = true **". This is with the emulator and using the ESC key for the back button. I tested on a real device and does the same thing.
Sorry but this also is not enough to understand.

Have you set a breakpoint (I would set it on
If KeyCode = KeyCodes.KEYCODE_BACK Then
)?

Then press F8
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I'll make it even simpler. What does this mean "Activity (main) Pause, UserClosed = true *"? What would cause that?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I am at a loss. It runs through the code and everything is fine. Then it runs the end sub of the KeyPress sub of the code below and everything disappears.
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
Select layout
        Case  "NewGame"
                allinvisible
                MainMenu
    End Select
End If
End Sub
 
Upvote 0

terryn

Member
Licensed User
Longtime User
You need to return "True" in Activity_KeyPress sub to consume the event and prevent the app from closing.


Terry
 
Upvote 0

terryn

Member
Licensed User
Longtime User
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
Select layout
        Case  "NewGame"
                allinvisible
                MainMenu
                Return True 'Consume the event
    End Select
End If
End Sub
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
When the user presses the back key I have a msgbox that pops up. How do I make it so the msgbox just goes away and it acts like the user never pressed the back button?
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Well when the user presses the back button a messagbox opens that asks them if they want to continue playing, go to the main menu or exit the application. So if they select continue playing I don't want anything to happen except exit the messagebox.
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Personally I hate the "Are you sure you want to quit" message .. if I didn't I would have not chosen the quit option . If I made a mistake I can always restart.
What I'm trying to say .. If your user presses the back button they have done so for a reason .. and it is Not to continue playing.

IMO ... Only give them the option of Menu or Exiting (from the menu cannot they re-continue play if it was a mistake ?)
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
The reason I put that is because I accidentally hit the back button too many times. Going to the main menu and then back into the game is not as easy as pressing Continue playing. So how do I just make it close the message box?
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Well when the user presses the back button a messagbox opens that asks them if they want to continue playing, go to the main menu or exit the application. So if they select continue playing I don't want anything to happen except exit the messagebox.

B4X:
Dim response As Int = Msgbox2("Choose your Option","Options","Main Menu","Cancel","Exit App",Null)    ' Cancel or Continue Play
  
   Log (response)
  
   Select response
      Case DialogResponse.POSITIVE
         Log ("Main Menu")    'show main menu
      Case DialogResponse.NEGATIVE
         Log ("Exit App")    'exit the app
     'Case DialogResponse.CANCEL
        'Log ("Cancel")       'Do Nothing ?? >> depending on your code prior to showing this Message Box this Case is not needed
    End Select
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I thought the continue playing was working but it closes the application.
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean

If KeyCode = KeyCodes.KEYCODE_BACK Then

Select layout
        Case  "NewGame"
                allinvisible
                MainMenu
                 Return True
        Case  "Options"
                allinvisible
            MainMenu
             Return True
        Case  "GamePlay"
        Dim result As Int
result =    Msgbox2("Exit Game?", "Exit", "Exit", "Main Menu", "Continue playing",Null)
            If result = DialogResponse.Positive Then
Msgbox(gmid,"gameid")
                ExitApplication
        'else if result = DialogResponse.NEGATIVE Then
            'continue playing
            'Return
        else If result = DialogResponse.CANCEL Then
                 'mainmenu
            allinvisible
            MainMenu
             Return True
                End If
        Case  "LoadGame"
                allinvisible
            MainMenu
             Return True
        Case "Mainmenu"
            ExitApplication
        Case "HowtoPlay"
                allinvisible
            Options
             Return True
        Case Else
            Msgbox("Need to create a backbutton for this","test")
    End Select
End If
If KeyCode = KeyCodes.KEYCODE_HOME Then
    Msgbox("HOME","home")
   
End If
End Sub
 
Upvote 0
Top