Android Question B4xpages, cross page variables

james_sgp

Active Member
Licensed User
Longtime User
Hi, having an issue; that's seems so obvious, yet I can`t see my error.

I have declared a public variable in 'B4xMainPage'

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Public ques_level As Int = 0

When I try to access 'ques_level' on B4Xpage 'win_page' using:
B4X:
private Sub B4XPage_appear
    Log(MP.ques_level)

I am not getting the most recent value of the variable, I`m only getting the first value I set in (in 'B4xMainPage')...e.g. at first I set to 3, and I get 3 on the other page; but when I then change it t 2 on 'B4XMainPage' I still only get 3 on 'win_page'?

Would appreciate someone pointing out my stupidity, please :oops:

Thanks, James
 

james_sgp

Active Member
Licensed User
Longtime User
Here is a sample app that has the same issue.
 

Attachments

  • variable_issue.zip
    11.2 KB · Views: 164
Upvote 0

pliroforikos

Active Member
Licensed User
MP in your first code tells me that you have declare a new instance of MainPage and so the ques_level=0
as
first value.

You shoulfd use log(B4XPages.MainPage.ques_level)
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Thanks for the suggestion, it kinda made sense. However, doesn`t give the desired result, see screenshot
 

Attachments

  • Capture.PNG
    Capture.PNG
    32.7 KB · Views: 162
Upvote 0

agraham

Expert
Licensed User
Longtime User
If i try to use b4xmanpage as u suggested it is not recognised...
Yea, sorry. I forgot B4XMainPage was a class not an Activity. I don't use B4XPages at the moment. As an oldie set in my ways I find the old way with individual Activities simpler.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
What you are doing seems convoluted and unnecessary but your problem is that you only set your MP variable once in your single instance of win_page, when it is created, but you are creating further instances of B4XMainPage so your MP variable only refers to the first instance of B4XMainPage, where ques_level = 1, and not to your newest instance.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
To refer to the B4XMainPage (from within another page); you don't need to create a variable for it or add it to the B4XPages, it's already there:
B4X:
B4XPages.MainPage.ques_level = B4XPages.MainPage.ques_level * 7
 

Attachments

  • Test2.zip
    11.2 KB · Views: 152
Upvote 0

agraham

Expert
Licensed User
Longtime User
To refer to the B4XMainPage (from within another page); you don't need to create a variable for it
In this case he is creating multiple instances (probably a bad idea) of B4XMainPage and B4XPages.MainPage is again set only once when B4xPagesManager is Initialized so that will only ever refer to the initial instance.
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
SOLVED...
Great thanks for all the feedback, and the solution. I`m new to B4Xpages (my 2nd app), and was basing things on the B4Xpages triple page example; which I`d obviously misinterpreted at some point.

Stay safe... ?
 
Upvote 0
Top