Android Question Retrieve the variable of another page with B4XPages

Justcooldev

Member
Licensed User
Hello, I would like to know how to retrieve an variable of another page with B4XPages. I succeeded (the code is at the bottom?), it works 1 time and then it blocks as if the page does not refresh.
B4A - Code:
Dim aa As mypage = B4XPages.getPage("my page")
lbl2.Text = aa.ImmTag
Thanks in advance.
 
Solution
To prevent more likes for post 5 let we help Justcooldev how it works in B4J which is the preferred B4Xpages program IDE. The trick is that B4XPages are a class so we can referred to it in B4XMainPage. After changing the value the new value is also updated.
B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
'    --- Give access from other B4XPages
    Public OP As B4XOtherPage
    Public MP As B4XMainPage
    Public MPstr As String
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    B4XPages.SetTitle(Me, "Main...

Justcooldev

Member
Licensed User
I have did this :
B4A:
Public lbl2 As Label
Dim aa As mypage = B4XPages.getPage("mypage")
But it d'ont works, pleese help...
 
Last edited:
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
To prevent more likes for post 5 let we help Justcooldev how it works in B4J which is the preferred B4Xpages program IDE. The trick is that B4XPages are a class so we can referred to it in B4XMainPage. After changing the value the new value is also updated.
B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
'    --- Give access from other B4XPages
    Public OP As B4XOtherPage
    Public MP As B4XMainPage
    Public MPstr As String
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    B4XPages.SetTitle(Me, "Main Page")
    
    OP.Initialize
    B4XPages.AddPage("OtherPage", OP)
    
'    --- Define variable and access
    MPstr = "This is B4XMainPage variable"
    MP = B4XPages.MainPage
    
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    Log($"Content from B4XOtherPage: ${OP.OPstr} in B4XMainPage"$)
    B4XPages.ShowPage("OtherPage")
End Sub

B4XOtherPage:
Sub Class_Globals
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    
'    --- Give access from other B4XPages
    Public OPstr As String
    Private lbl2 As B4XView
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
'    --- Define varible in B4XOtherPage
    OPstr = "This is from B4XOtherPages"
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("OtherPage")
    B4XPages.SetTitle(Me, "Other Page")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    Log($"Content in B4XMainPage: ${B4XPages.MainPage.MPstr} from B4XOtherPage"$)
    Log($"This works also: ${B4XPages.MainPage.OP.OPstr}"$)
    OPstr = "B4XOtherPage is changed"
    lbl2.Text = OPstr
    Log($"This works also: ${B4XPages.MainPage.OP.OPstr}"$)
End Sub
 
Upvote 0
Solution

Justcooldev

Member
Licensed User
Hello, Thank you very much for your help @MicroDrie, I had been struggling for 2 days... In fact the problem was simple to solve but as I started recently B4A It was not easy to find: It was enough to do as below :
B4A:
Private aa As mypage = B4XPages.getPage("mypage")
Sub B4XPage_Appear 'when the page appear, not created :
    lbl2.Text = aa.Myvariable 'Myvariable is public into the other page
End Sub
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
I'm glad you found the solution. There is a lot of information to be found on this active forum. Just use one or more keywords in a search. It is simply a matter of reading a lot, and with the answers given you often find very instructive solutions as by-catch. a tip to bookmark things that you accidentally found as by-catch because it is very difficult to find them later! Good luck and have fun with your programming.
 
Upvote 0

Justcooldev

Member
Licensed User
I'm glad you found the solution. There is a lot of information to be found on this active forum. Just use one or more keywords in a search. It is simply a matter of reading a lot, and with the answers given you often find very instructive solutions as by-catch. a tip to bookmark things that you accidentally found as by-catch because it is very difficult to find them later! Good luck and have fun with your programming.
Thank you so much for your encouragements :)
 
Upvote 0
Top