B4J Question [SOLVED] How To Run Sub On Window or App Close from B4X_ClosePage

cklester

Well-Known Member
Licensed User
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.

What am I missing?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
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
 
Upvote 0

cklester

Well-Known Member
Licensed User
Calling B4XPages.ClosePage(Me) will not raise the CloseRequest event.

You should do something like...

Excellent. It works for me now.

Is there something in Java that prevents the CloseRequest event being called because of a ClosePage() call?
 
Upvote 0
Top