Android Question Simplest way to know if B4XPage_Appear was triggered by B4XPages.ClosePage

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I can do this but wonder what the simplest (least amount of code) way is to do this for a large project.

RBS
 

LucaMs

Expert
Licensed User
Longtime User
I can do this but wonder what the simplest (least amount of code) way is to do this for a large project.

RBS
Strange question.

I assume you mean how to detect if page x's Appear event is triggered by closing page y.

"Appear" will be triggered in that case or by the Show method; if instead the page will be displayed because the app will return to the foreground, you will have to handle the Foreground event.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Strange question.

I assume you mean how to detect if page x's Appear event is triggered by closing page y.

"Appear" will be triggered in that case or by the Show method; if instead the page will be displayed because the app will return to the foreground, you will have to handle the Foreground event.
> Strange question.

Maybe I didn't tell enough about the context of this.
I need to know this in the Sub B4XPage_Appear of every B4XPage.
The reason for this is that if that Sub was triggered by B4XPages.ShowPage (This is done by a Sub in B4XMainPage called ShowPage) then a Sub will run in that
B4XPage if not (so triggered by B4XPages.ClosePage) then that Sub won't run and some other code needs to run from Sub B4XPage_Appear.

In any case I have some simple code that will detect these 2 routes and am testing this now.

RBS
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
One way could be this (not a few lines):
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
  
    Private Const MY_PAGE_ID As String = "PageOne"
    Private ShowCalled As Boolean
End Sub
B4X:
Private Sub B4XPage_Appear
    If ShowCalled Then
    Else
    End If
    ShowCalled = False
End Sub
B4X:
Public Sub Show
    ShowCalled = True
    B4XPages.ShowPage(MY_PAGE_ID)
End Sub
Calling that Show method instead of B4XPages.ShowPage(...).



Another could be to create a public variable in the B4XMainPage, setting its value to the id of the page you just closed:
B4XMainPage:
Public ClosingPageID As String

PageX:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
  
    Private Const MY_PAGE_ID As String = "PageOne"
End Sub
PageX:
Private Sub B4XPage_Appear
    If B4XPages.MainPage.ClosingPageID.Length > 0 Then
    Else
    End If
    B4XPages.MainPage.ClosingPageID = ""
End Sub
PageX:
Private Sub B4XPage_Disappear
    B4XPages.MainPage.ClosingPageID = MY_PAGE_ID
End Sub
 
Last edited:
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
One way could be this (not a few lines):
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
 
    Private Const MY_PAGE_ID As String = "PageOne"
    Private ShowCalled As Boolean
End Sub
B4X:
Private Sub B4XPage_Appear
    If ShowCalled Then
    Else
    End If
    ShowCalled = False
End Sub
B4X:
Public Sub Show
    ShowCalled = True
    B4XPages.ShowPage(MY_PAGE_ID)
End Sub
Calling that Show method instead of B4XPages.ShowPage(...).



Another could be to create a public variable in the B4XMainPage, setting its value to the id of the page you just closed:
B4XMainPage:
Public ClosingPageID As String

PageX:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
 
    Private Const MY_PAGE_ID As String = "PageOne"
End Sub
PageX:
Private Sub B4XPage_Appear
    If B4XPages.MainPage.ClosingPageID.Length > 0 Then
    Else
    End If
    B4XPages.MainPage.ClosingPageID = ""
End Sub
PageX:
Private Sub B4XPage_Disappear
    B4XPages.MainPage.ClosingPageID = MY_PAGE_ID
End Sub
OK, I did something similar (I think):

B4X:
'In B4XMainPage, ClassGlobals:
    Public mapB4XPagesShow As Map 'Will be initialized in Public Sub Initialize

'In Sub ShowPage in B4XMainPage:
mapB4XPagesShow.Put(iPage, 1) 'iPage is an integer number identifying the various B4XPages

'In the various B4XPages in Sub B4XPageAppear (cMP is a class instance of B4XMainPage):
    If 0 = cMP.mapB4XPagesShow.GetDefault(iPageCode, 0) Then 'this means this Sub was triggered by B4XPages.ClosePage
        'Run some code here
    Else
        'this means this Sub was triggered by B4XPages.ShowPage
        cMP.mapB4XPagesShow.Put(iPageCode, 0) 'return to the default
    End If

Tested this now and seems to work all fine.

RBS
 
Upvote 0

Spavlyuk

Active Member
Licensed User
Depending on what you're trying to do, you could potentially use resumable subs.

page1:
Private Sub WaitForValue
    Wait For (B4XPages.MainPage.Page2.Show) Complete (Result As Object)
    If Result <> Null Then
        ' ...
    End If
End Sub

page2:
Public Sub Show As ResumableSub
    Wait For Value_Selected (Result As Object)
    Return Result
End If

Private Sub B4XPage_CloseRequest As ResumableSub
    CallSubDelayed2(Me, "Value_Selected", Null)
    Return True
End Sub

Private Sub CustomListView_ItemClick(Index As Int, Value As Object)
    B4XPages.ClosePage(Me)
    CallSubDelayed2(Me, "Value_Selected", Value)
End Sub
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Depending on what you're trying to do, you could potentially use resumable subs.

page1:
Private Sub WaitForValue
    Wait For (B4XPages.MainPage.Page2.Show) Complete (Result As Object)
    If Result <> Null Then
        ' ...
    End If
End Sub

page2:
Public Sub Show As ResumableSub
    Wait For Value_Selected (Result As Object)
    Return Result
End If

Private Sub B4XPage_CloseRequest As ResumableSub
    CallSubDelayed2(Me, "Value_Selected", Null)
    Return True
End Sub

Private Sub CustomListView_ItemClick(Index As Int, Value As Object)
    B4XPages.ClosePage(Me)
    CallSubDelayed2(Me, "Value_Selected", Value)
End Sub
Not sure how to use this and it seems more code than what I got now.
I simplified (less code) my code further like this:

In B4XMainPage this, where miPageCode was already set by Sub B4XPage_Appear in the various pages:
B4X:
Sub PageAppearedFromClosePage As Boolean
   
    Dim bResult As Boolean
   
    bResult =  (0 = mapB4XPagesShow.GetDefault(miPageCode, 0))
   
    If bResult = False Then
        mapB4XPagesShow.Put(miPageCode, 0) 'so return to the default
    End If
   
    Return bResult
   
End Sub

Now I can do in the Sub B4XPage_Appear in the various pages:
B4X:
    If cMP.PageAppearedFromClosePage Then 'this means this Sub was triggered by B4XPages.ClosePage
        'run some page code
    End If

Don't think it can be done with any less code.

RBS
 
Upvote 0
Top