B4J Question Is there a mechanism in B4J to intercept the closing of a window?

BlueVision

Active Member
Licensed User
Longtime User
In a larger B4J project I have integrated the display of a programme description in an extra panel. This works very well so far.
Originally I had planned to display this PDF-file directly from the assets. ReadOnly access is supposedly possible. But it doesn't really work, because the file is apparently opened for display with read/write access, although nothing is written to the file. Ok, so I copy the file from the assets before displaying it and then that works too. But now I would like to delete this temporary file again when the programme ends. This can be done with the routine MainForm_Closed. However, this only works when the programme is closed regularly. If the user closes the programme while the file is open in the Reader by closing the window, the file is still in access and cannot be deleted. Is there a workaround for this case? I imagine that with ANDROID this was possible by pressing the BACK key or the MAIN key. Is there a similar function under B4J? Is it possible to intercept the closing of the window programmatically and still carry out a few actions?
 

peacemaker

Expert
Licensed User
Longtime User
If pure B4J app - use _CloseRequest event.

B4X:
Sub MainForm_CloseRequest (EventData As Event)
...

B4Xpage app:
Sub B4XPage_CloseRequest As ResumableSub
    Progress(False)    'close progressbar
    CallSub(B4XPages.GetPage("usbPage"), "Disconnect_All")    'disconnect ports
    #if B4J
        If BTstream.IsInitialized Then
            currentConnection.Disconnect    'stop Bluetooth connection
            BTstream.Close
        End If
        agPage = Null
    #end if
    usbPage = Null    'close other B4Xpages

End Sub
 
Last edited:
Upvote 0

BlueVision

Active Member
Licensed User
Longtime User
Nice one...
I don't know if I have understood this correctly, but surprisingly it works... To make it easier to understand. It is the PDFBoxWrapper (wonderful by the way, thanks to knutf for this great software!).
In the B4XMainPage a variable is defined to control the class that identifies the PDF:
B4X:
Public PDF As PDFBoxWrapper
with
B4X:
PDF.CloseDocument
I unload the wrapper. The problem is that you cannot easily reach this variable via MainForm_CloseRequest or MainForm_Close. I have now also defined the corresponding variable as a mirror in the main.
B4X:
Public PDFMain as PDFBoxWrapper
After initialising the variable in the B4XMainPage, I assign the same value from the B4XMainPage to the mirrored variable defined in the main.
B4X:
Main.PDFMain = PDF
If I then control this mirrored variable via MainForm_CloseRequest, it works perfectly. I'm not sure, this might not be quite the correct way, but it definitely works.
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
@BlueVision
If I understand you correctly, you defined the variable as public in B4XMainPage and you battle to reach it...

Try this B4XPages.MainPage.PDF - you should be able to reach all your public defines using this syntax...

Don't worry - took me a while to figure that one out as well...
 
Upvote 0

BlueVision

Active Member
Licensed User
Longtime User
@BillMeyer
Exactly what I did. I will try this... Exactly these kinds of things are not necessarily my "favourites" when I write a programme. On the contrary, I usually avoid getting into such dependencies due to lack of knowledge. That is always the problem here in the forum. How exactly do you define a problem in order to have a hit in the forum? In addition then of course not in the mother tongue. Hopeless. I'm sure the topic has already been discussed elsewhere, also with regard to the dependencies of the individual programme modules on each other. It is sometimes also difficult to impart knowledge. Where do you start, where do you stop? This is not meant against Erel or other specialists here in the forum. The problem is simply how to get the knowledge to the people who are looking for it? The crucial thing is that you understand it, then you won't ask stupid questions like I did.

Edit: Thanks Bill. Works perfectly. A code smell less for today...
 
Last edited:
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
Just remember - a question is never stupid - a heart surgeon was once a first year student !!!
 
Upvote 0
Top