Android Question B4XPages Code

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello all,

it's known that the best place to put startup code to check parameters and data for app initialization (like creation tables on new databases, environment variables and so on) in Android/B4A is in the start service, as well as the right choice to put global variables is in the "Process Global" in Main module . But using B4XPages there is no start service and isn't recommended to put much code in Main module. Then, where should I put database and other initialization code or global variables if I'm using B4XPages in order to get the most compatible code as possible between platforms ?

Thanks!
 

William Lancee

Well-Known Member
Licensed User
Longtime User
A B4XPage is a just a class. You can instantiate that class once or multiple times in B4XMainPage or in any other class. Each instantiation gets its own resources, which are
defined in Public Sub Initialize and Private Sub B4XPage_Created of the class. I use Initialize for variables that are passed down as parameters and Created for other variables.

Sometimes I put Public variables in the MainPage Class_Globals which is accessible from anywhere with B4XPages.MainPage.varname. These variables will be loaded in B4XPage_Created of of the B4XMainPage class.

At other times it is more appropriate to put the variables in named pages or non-page classes, where they can be accessed through Public variables as properties or through Public Sub as methods. For example you may want to put a database and its access in a non-page class and access it through Public properties and methods.

The beauty of a class is that although it is defined once, each instance keeps its own resources, for example loading a layout in one instance does not affect the layout of another instance.

Other members of the community may have other suggestions.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
A B4XPage is a just a class. You can instantiate that class once or multiple times in B4XMainPage or in any other class. Each instantiation gets its own resources, which are
defined in Public Sub Initialize and Private Sub B4XPage_Created of the class. I use Initialize for variables that are passed down as parameters and Created for other variables.

Sometimes I put Public variables in the MainPage Class_Globals which is accessible from anywhere with B4XPages.MainPage.varname. These variables will be loaded in B4XPage_Created of of the B4XMainPage class.

At other times it is more appropriate to put the variables in named pages or non-page classes, where they can be accessed through Public variables as properties or through Public Sub as methods. For example you may want to put a database and its access in a non-page class and access it through Public properties and methods.

The beauty of a class is that although it is defined once, each instance keeps its own resources, for example loading a layout in one instance does not affect the layout of another instance.

Other members of the community may have other suggestions.
Thanks @William Lancee ,

using this logic approach, then the code in B4A Starter should be moved to B4XMainPage Class_Globals right? I just started to translate our B4A apps to B4X Pages and must to confess that I still have some doubts because if an approach works doesn't necessarily means that will be the best to reduce specific platform coding... for example:

- Firebase Service/Notifications: what's the best approach in B4X Pages?
- Process Variables: in Android starter is Always active when the app is running, doesn't matter what activity is being shown. And in B4XPages? Is there a secure storage for persistent variables?
- Notifications: for now, the path is maintain two code versions in notifications calls (or three if we are talking about B4J also)... is it correct?

Still researching!

Thanks!
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
The pages context will not be paused or destroyed, until the process itself is killed.
You can save new data and status in the B4XPage_Background event before the process is killed by the OS.

I don't use notifications, but I don't see why you couldn't put them in any class you like.

The best approach is to create a trial project without bells or whistles and become familiar with it all. Then add things one at a time and test each.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
The pages context will not be paused or destroyed, until the process itself is killed.
You can save new data and status in the B4XPage_Background event before the process is killed by the OS.

I don't use notifications, but I don't see why you couldn't put them in any class you like.

The best approach is to create a trial project without bells or whistles and become familiar with it all. Then add things one at a time and test each.
Thanks @William Lancee šŸ‘
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Firebase Service/Notifications: what's the best approach in B4X Pages

 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
For those of us who feel comfortable with B4X, I urge you to take another look at the booklets that @klaus and @Erel prepared, especially the cross-platform one.
You can easily download the zip file from the Home page. That way you have the examples as well as the ability to cut and paste.

I looked again and learned some new tricks. For example how to switch to another platform inside the IDE!

Very impressive work by these authors.
 
Upvote 0
Top