Android Question [B4XPages] How know if a page already exist ?

Mattyeux

Member
Licensed User
Hi,
I'm converting my app to B4XPages and i want to know if a page already exist ,is there a way to do it?

Something like that:
Dim MyPage As B4XMyPage
MyPage.Initialize
if B4xPages.PageExist("MyPage") then
    MyPage = B4xPages.GetPage("MyPage")
else
    B4XPages.AddPageAndCreate("MyPage",MyPage)
end if


Regards
 

John Naylor

Active Member
Licensed User
Longtime User
I've just started delving in to B4XPages (literally today) so this may or may not help or there may be a way better way but you could maybe try the following...


B4X:
Try
    Dim MyPage As B4XMainPage = B4XPages.GetPage("PageNameToCheck")
    Log ("Page exists")
Catch
    Log ("Page doesn't exist")
    B4XPages.AddPageAndCreate("PageNameToCheck",MyPage)
End Try
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'm converting my app to B4XPages and i want to know if a page already exist ,is there a way to do it?
I've just started delving in to B4XPages
Me too, so I don't know if a similar function exists but you could create a Map with references to all your B4XPages.
When you create and add the page to B4XPages:
B4X:
    B4XPages.AddPage("Page7", Page7)
    mapPages.Put("Page7", Page7)


    If mapPages.ContainsKey("Page7") Then

Another way could be:
B4X:
Public Sub PageExists(PageID As String) As Boolean
    Dim Result As Boolean = True

    Try
        B4XPages.GetPage(PageID)
    Catch
        Result = False
    End Try

    Return Result
End Sub
but you will get an error message in the log (although the function will work without crashing your app).

Note that if your need was to avoid to add a page more than once, there are no problems, because B4XPages.AddPage and B4XPages.AddPageAndCreate do not add duplicates.


P.S. All this, as Erel wrote, is not needed, since you cannot remove a page from B4XPages (I did not know, I checked only now), you can only Close a page.
 
Last edited:
Upvote 0

Mattyeux

Member
Licensed User
Thanks for your answers,
Why is it needed?
I use a B4XPage_BeforeAppear method to execute some code between create and show. If i add all pages with addPage when the program starts, B4XPage_Created is executed at the same time of showPage and i can't execute code between create and show. Am i right?

P.S. All this, as Erel wrote, is not needed, since you cannot remove a page from B4XPages (I did not know, I checked only now), you can only Close a page.
Yes, but I do "addPageAndCreate" each time followed by a getPage and I have messages in the logs "Page with this id already exists:", I don't know if this is serious but I just wanted to have a clean code

I think I will choose the "mapPage" solution but it would be better if we could access the list of pages directly in B4XPages.

Regards
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Am i right?
No. B4XPage_Created will only be raised right before the page becomes visible (unless you add it with AddPageAndCreate).

Yes, but I do "addPageAndCreate" each time followed by a getPage and I have messages in the logs "Page with this id already exists:", I don't know if this is serious but I just wanted to have a clean code
You should never see such message. This is a programming mistake.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…