Android Code Snippet B4XPages - Simple tip for referencing pages

This is a tip to save you some time. It is just an easier way to access your B4XPages classes. In the Main module, I put the page declarations in a custom type. Here is an example:

B4X:
Type PageContainerType (PageMainMenu As B4XPageMainMenu, PageSettings As B4XPageSettings, PageWelcome As B4XPageWelcome, PageEmailLogin As B4XPageEmailLogin, PageCreateEmailAccount As B4XPageCreateEmailAccount, PageSyncAdmin As B4XPageSyncAdmin, PageTasksToday As B4XPage_TodaysTasks, Task_Reading As B4XPageTask_Reading, Task_Video As B4XPageTask_Video, Task_SleepLog As B4XPageTask_SleepLog, Task_GetUpTime As B4XPageTask_GetUpTime, Task_Reminder As B4XPageTask_Reminder)

Then when you initialize the pages, do it like this:

B4X:
    Pages.Initialize
    
    Pages.PageMainMenu.Initialize
    B4XPages.AddPage("Page Main Menu", Pages.PageMainMenu)
    Pages.PageSettings.Initialize
    B4XPages.AddPage("Settings Page", Pages.PageSettings)

Then it is easier to reference your pages from other modules.

1633128794567.png
 

William Lancee

Well-Known Member
Licensed User
Longtime User
I like it, but it doesn't quite work in B4J. A more general approach would be to put this wrapper in B4XMainPage and refer back to B4XMainPage.

'B4XMainPage
B4X:
Sub Class_Globals
    Type PageContainerType (Page1 As testPage1, Page2 As testpage2)
    Private Root As B4XView
    Private xui As XUI
    Public pages As PageContainerType
End Sub

Public Sub Initialize
    pages.Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    pages.Page1.Initialize
    B4XPages.AddPage("p1", pages.Page1)
    pages.Page2.Initialize
    B4XPages.AddPage("p2", pages.Page2)
End Sub

Private Sub Button1_Click
    B4XPages.ShowPage("p1")
End Sub

' testPage1 Class
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private MP As B4XMainPage
End Sub

Public Sub Initialize As Object
    MP = B4XPages.MainPage
    Return Me
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    MP.pages.page2.logSuccess        'Log: success
    'Same as...
    B4XPages.GetPage("p2").As(testpage2).logSuccess
End Sub

'testPage2 Class
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private MP As B4XMainPage
End Sub

Public Sub Initialize As Object
    MP = B4XPages.MainPage
    Return Me
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
End Sub

Public Sub logSuccess
    Log("Success")
End Sub
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Addendum: If like me, you hate repetitive typing, here is a way to avoid that.

B4X:
Sub Class_Globals
    Type PageContainerType (Page1 As testPage1, Page2 As testpage2)
    Private TypeAsString As String = $"
    Type PageContainerType (Page1 As testPage1, Page2 As testpage2)
    "$
    Private Root As B4XView
    Private xui As XUI
    Public pages As PageContainerType
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    pages = InitPageContainer
End Sub

And a simple helper Sub.
B4X:
Public Sub InitPageContainer As PageContainerType
    Dim container As PageContainerType
    container.Initialize
    Dim v() As String = Regex.Split(" ", TypeAsString.Replace("  ", " ").Replace(CRLF, "").Replace("_", "").Replace(TAB, ""))
    Dim nameList As List: nameList.Initialize
    For i = 0 To v.Length - 2
        If v(i + 1) = "As" Then
            nameList.Add(v(i).trim.Replace("(", ""))
        End If
    Next
    For i = 0 To nameList.Size - 1
        Log($"container.${nameList.Get(i)}.Initialize: B4XPages.AddPageAndCreate("${nameList.Get(i)}", container.${nameList.Get(i)})"$)
    Next
    
    'lines copied from log (copy all of log and delete what you don't need - selective copying would be great, but it isn't there yet)
    container.Page1.Initialize: B4XPages.AddPageAndCreate("Page1", container.Page1)
    container.Page2.Initialize: B4XPages.AddPageAndCreate("Page2", container.Page2)
    Return container
End Sub

'testPage1 Class

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private MP As B4XMainPage
End Sub

Public Sub Initialize As Object
    MP = B4XPages.MainPage
    Return Me
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
End Sub

Private Sub B4XPage_Appear
    MP.pages.Page2.logSuccess
    'Same as...
    B4XPages.GetPage("Page2").As(testpage2).logSuccess
End Sub

'testPage2 Class

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private MP As B4XMainPage
End Sub

Public Sub Initialize As Object
    MP = B4XPages.MainPage
    Return Me
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
End Sub

Public Sub logSuccess
    Log("Success")
End Sub
 
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime User
After implementing this approach in an App I'm working on, I made some improvements.
#3 now shows the revised version
 
Top