Android Question B4XPages & shared constants

luke2012

Well-Known Member
Licensed User
Longtime User
Hi all,
within a B4XPages framework (from @Erel) app I need to share some (app) global constants. For example:

B4X:
    Public Host As String = "https://acme.com"
    Public WSNodeURL As String = Host & "/node/"
    Public WSFormat As String = "?_format=json"

MY GOAL
I need to access them within all B4A and B4i components (modules, services, instanced classes). So I need a single and reliable point of access within both platform (Android and iOS).

WITHIN ANDROID
Within B4A app I can use "Starter" service to set the above public constants.

CROSS-PLATFORM ?
How I can set this within a cross platform application in order to refer to a single point of access for the above constants (so I can call "Starter.WSNodeURL " within B4A and B4i without code changes between the two platforms ?
Can I create a "Starter" code module within B4i and reference them like I do within B4A with "Starter.WSNodeURL" ?

B4X:
'B4i Code module
Sub Process_Globals
    Public Host As String = "https://acme.com"
    Public WSNodeURL As String = Host & "/node/"
    Public WSFormat As String = "?_format=json"
End Sub


Thanks in advance for your precious help :)
Luca.
 

luke2012

Well-Known Member
Licensed User
Longtime User
Excellent.

This is exactly the purpose of B4XPages.GlobalContext. You can assign anything you like to this field, including a class instance or a custom type, and access it from wherever you like.

In B4A, it makes sense to assign it in the starter service.

Thank you for this great tip :)

So for example. Can I assign in this way ?
B4X:
'STARTER SERVICE
#Region  Service Attributes 
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    
End Sub

Sub Service_Create
    Public AppGlobals As clsGlobals
    AppGlobals.Initialize
    B4XPages.GlobalContext = AppGlobals
End Sub

So, in B4A, it makes sense to assign it in the starter service.
What about B4i ? Where to assign it in a secure (alway accessible) way within an iOS App?
 
Last edited:
Upvote 0
Top