Android Question What's the best way to determine whether a page has been added to B4XPages or not?

Andris

Active Member
Licensed User
Longtime User
In B4XPages, I've been adding new pages with B4XPages.AddPages. What's the best way to see if a certain page ID already exists? If I try B4XPages.GetPage(id), it crashes, which I can Catch. But is there a simpler way?
 

walterf25

Expert
Licensed User
Longtime User
In B4XPages, I've been adding new pages with B4XPages.AddPages. What's the best way to see if a certain page ID already exists? If I try B4XPages.GetPage(id), it crashes, which I can Catch. But is there a simpler way?
I believe you can get a list of pages already added like so.
B4X:
    Dim set As B4XSet
    set = B4XPages.GetManager.mStackOfPageId

Walter
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Bad but working code:
B4X:
    Dim Added As Boolean
    Try
        Added = B4XPages.GetPage("YourPageIDHere") <> Null
    Catch
    End Try 'ignore
    Log("Added: " & Added)
It's strange that I had to use a Try-Catch, because the GetPage function returns an object and should return Null if the page doesn't exist, but without that Try the project would crash.

I think this is something Erel should change.
 
Upvote 0

epiCode

Active Member
Licensed User
A simple TestPage.Isinitialized will return if page is loaded or not
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim pg1 As page1
    pg1.Initialize
    
    If B4XPages.GetManager.FindPIFromB4XPage(pg1) = Null Then Log("Not added") Else Log("Added")        'Not added
    B4XPages.AddPage("pg1", pg1)
    If B4XPages.GetManager.FindPIFromB4XPage(pg1) = Null Then Log("Not added") Else Log("Added")        'Added
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Or as Sub...
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    Dim pg1 As page1
    pg1.Initialize
    
    Log(isPageAdded(pg1))    'false
    B4XPages.AddPage("pg1", pg1)
    Log(isPageAdded(pg1))    'true
End Sub

Private Sub isPageAdded(pge As Object) As Boolean
    Return Not(B4XPages.GetManager.FindPIFromB4XPage(pge) = Null)
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
A simple TestPage.Isinitialized will return if page is loaded or not
4XPages.GetManager.FindPIFromB4XPage(pg1) = Null
The question:
What's the best way to see if a certain page ID already exists?
"Page ID", not Page object.

This is why:
1729901121020.png
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
@LucaMs True.

How often do you name a page differently from its object name? You can, but do you do it? Isn't that confusing?

If you do then maybe you should keep your own map of names and objects.
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
One day I asked EREL how many pages can be added, for memory reasons, and he answered something like A LOT (I understood that I shouldn't worry about that). So I load all of them when I start my application and then forget if they are there or not. But of course, this question may be appropriate
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
One day I asked EREL how many pages can be added, for memory reasons, and he answered something like A LOT (I understood that I shouldn't worry about that). So I load all of them when I start my application and then forget if they are there or not. But of course, this question may be appropriate
However, if you add them via AddPageAndCreate and there are "many" of them, it will take some time, which may be annoying for the user.
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
Thank you very much for the clarification.
I just do the
B4XPages.AddPage
and when I need it
B4XPages.ShowPage("pagenname")

I have never used AddPageAndCreate and I must admit that I did not know about it.
As I see, this also causes the B4XPage_Created code to be executed
which can be very interesting.

When the form is launched, this event will already be executed and only the
Private sub B4XPage_Appear

Now I understand better the reason for this question.
 
Upvote 0
Top