In my original project which did not use B4XPages I was able to trap back key by following code in Main Activity
Objective here is trap back key and close drawer if open else ask if user wants to exit without saving
Sub Activity_KeyPress (KeyCode As Int) As Boolean
Select KeyCode
Case KeyCodes.KEYCODE_BACK
'close left drawer if open
If Drawer.LeftOpen Then
Drawer.LeftOpen = False
Return True
End If
'''code to confirm exit
Activity.Finish
End Sub
After moving the project to B4XPages it is structured like this
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
B4XPages.GetManager.RaiseEvent(B4XPages.GetManager.GetTopPage, "B4XPage_KeyPress", Array(KeyCode))
Return False
End Sub
Sub B4XPage_KeyPress(KeyCode As Int) As Boolean
.... same code as above .....
End Sub
Now when the back key is pressed, the code still gets triggered but application is minimized/paused first
and results in following error
** Activity (main) Resume **
<--- ((back key pressed))
** Activity (main) Pause event (activity is not paused). **
Error occurred on line: 1711 (B4XEdViewer)
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@2b8ac14 is not valid; is your activity running?