iOS Question [B4XPage] How to prevent page closing

Alexander Stolte

Expert
Licensed User
Longtime User
If the user closes the page but there are still unsaved changes, I would like to show the user a warning and give them the option to cancel the action.
How does this work with B4XPages?

Thanks
 

LucaMs

Expert
Licensed User
Longtime User
B4X:
Private Sub B4XPage_CloseRequest As ResumableSub
    Dim sf As Object = xui.Msgbox2Async("Do you really want to close?", "Title", "Yes", "Cancel", "No", Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result <> xui.DialogResponse_Positive Then
        Return True
    End If
    
    Return False
End Sub
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
I've been able to simulate the page close event on B4i with the following trick.
You can check if this allows the LucaMS suggestion to work for you

B4X:
Sub B4XPages_Disappear
    #if B4i
    Dim PageClosed As Boolean = Not(B4XPages.GetManager.mStackOfPageIds.Contains(B4XPages.GetPageId(Me)))
    If PageClosed Then
        B4XPage_CloseRequest
    End If
    #end if
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I don't have B4i, so what I'm about to write could be completely wrong.

I suppose there is a close event in the Main of a B4i project. In this, you should detect which B4XPage is currently displayed, call one of its methods asking the user (...) and possibly show another B4XPage.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
When that event is triggered on that page (even several times) it will always be in the stack, otherwise how could it be triggered?

1711648567646.png


:oops:
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
the Disappear event is triggered _after_ the page has been closed, so it's not on the stack anymore.
maybe not exactly what Alexander was searching, but it could be starting point
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can prevent the user from closing the page, but if this option is enabled then you aren't expected to cancel it. The user can close the page with a swipe.

B4X:
B4XPages.GetNativeParent(Me).HideBackButton = True
Another option is to show the page with B4XPages.ShowPageAndRemovePreviousPages.

You can then add a "close" button and handle its event.
 
Upvote 0
Top