Android Question Keycode_Menu Crash

An Schi

Well-Known Member
Licensed User
In belows code i can press the back key as often as i want to and it works just fine. But when i press the menu key my app crashes. This crash can happen after the first, second or third click. I cannot understand this behaviour, because the executed code for both keys is the same.
I'd be happy if you could help me out :)

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean

  If KeyCode = KeyCodes.KEYCODE_BACK Then
     If PanelWithSidebar.IsSidebarVisible = True Then
       PanelWithSidebar.CloseSidebar
     Else
       PanelWithSidebar.OpenSidebar
     End If
     
   Else If KeyCode = KeyCodes.KEYCODE_MENU Then
     If PanelWithSidebar.IsSidebarVisible = True Then
       PanelWithSidebar.CloseSidebar
     Else
       PanelWithSidebar.OpenSidebar
     End If
   End If
   
   Return True
   
End Sub


P.s.: The PanelWithSidebar stuff is from @Informatix and can be found here: https://www.b4x.com/android/forum/threads/class-slidingsidebar.21533/
 

An Schi

Well-Known Member
Licensed User
How do i get them?
When i test it via bridge in debug mode, there are no logs about the crash in the IDE.
My phone only says 'blabla has stopped'
 
Upvote 0

An Schi

Well-Known Member
Licensed User
Thanks! :)
The CallSubDelayed solution is working. Below is my whole construct in case someone will dig this out in the future....


B4X:
 Private KC As String

Sub Activity_KeyPress (KeyCode As Int) As Boolean
   If KeyCode = KeyCodes.KEYCODE_BACK Or KeyCode = KeyCodes.KEYCODE_MENU Then
     KC = KeyCode
     CallSubDelayed(Me, "HandleSideBar")
     Return True
   End If
   Return False
End Sub

Sub HandleSideBar
   If KC = KeyCodes.KEYCODE_MENU Then
     If PanelWithSidebar.IsSidebarVisible = True Then
       PanelWithSidebar.CloseSidebar
     Else
       PanelWithSidebar.OpenSidebar
     End If
   Else If KC = KeyCodes.KEYCODE_BACK Then
       If PanelWithSidebar.IsSidebarVisible = True Then
         PanelWithSidebar.CloseSidebar
       Else
         If WV1Extra.CanGoback = True Then
           WV1.Back
         Else
           Activity.Finish
         End If
       End If
   End If
End Sub
 
Upvote 0
Top