I allow my users to use the Escape key to close windows and shut down the app. For one of the app windows (not the main window), I intercept it like so:
B4X:
Sub KeyPressed_Filter (e As Event)
Dim jo As JavaObject = e
Dim EventType As String = jo.RunMethodJO("getEventType", Null).RunMethod("getName", Null)
If EventType = "KEY_RELEASED" Then
Dim keycode As String = jo.RunMethod("getCode", Null)
If keycode = "ESCAPE" Then
B4XPages.ClosePage(Me)
e.Consume
End If
End If
End Sub
I have a B4XPages_CloseRequest() Sub set up that is not getting triggered by the B4XPages.ClosePage(Me) call in the above Sub.
B4X:
Private Sub B4XPage_CloseRequest As ResumableSub
Log("Closing the window!")
Return True
End Sub
The B4XPage_CloseRequest Log output never happens.
Calling B4XPages.ClosePage(Me) will not raise the CloseRequest event.
You should do something like:
B4X:
Sub KeyPressed_Filter (e As Event)
Dim jo As JavaObject = e
Dim EventType As String = jo.RunMethodJO("getEventType", Null).RunMethod("getName", Null)
If EventType = "KEY_RELEASED" Then
Dim keycode As String = jo.RunMethod("getCode", Null)
If keycode = "ESCAPE" Then
e.Consume
Wait For (B4XPage_CloseRequest) Complete (ShouldClose As Boolean)
If ShouldClose Then B4XPages.ClosePage(Me)
End If
End If
End Sub
Form_CloseRequest event is raised when the user closes the form. It is not raised when it is closed programmatically.
This affects the B4XPage_CloseRequest event.