Android Question [B4xPages] How to use GlobalContext?

udg

Expert
Licensed User
Longtime User
Hi all,
I'm in the process of converting a huge B4J Windows-only project to B4XPages and then Android.
The original project had a Static Module used as a "repository" for common variables and functions (even those requiring events).
First step was to define a Standard Class and copy everything from the static module to it (so that http jobs could work)

The clsCommon class is initialized in the Starter service. I added the following line:
B4XPages.GlobalContext = AppGlobals
where AppGlobals is the instance of the clsClass as defined in the Starter module.

Question: how do I call a function in that class (e.g SilentSync)?
Wait For(B4XPages.GlobalContext.SilentSync) Complete (Result As Object)
gives error while
Wait For(Starter.AppGlobals..SilentSync) Complete (Result As Object)
looks not the right way to keep B4A and B4J aligned.

Note: I can think of many ways to "solve the problem" and have a working app, but I'd like to learn how to use this specific object (GlobalContext) and write code thts' almost 100% shared between B4A and B4J. Thank you for your help.
 

LucaMs

Expert
Licensed User
Longtime User
The clsCommon class is initialized in the Starter service. I added the following line:
B4XPages.GlobalContext = AppGlobals
where AppGlobals is the instance of the clsClass as defined in the Starter module.
You know that B4J does not have the Starter service module.


Why not just create a public instance of the class in the B4XMainPage?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Why not just create a public instance of the class in the B4XMainPage?
It was my first thought, but then I was curious on how to use the GlobalContext object.

@Erel : given it's not needed in this specific case, can you show a couple of lines of code on how to properly use it when needed? Thanks.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4X:
Dim at As ActualType = B4XPages.GlobalContext
Too synthetic for my only two neurons šŸ˜„ :(

If I understand correctly, GlobalContext is a public object variable, which you must (if you want to use it, of course) assign your own object.

This means that you can use it as you wrote in post #3 by creating the object in the Starter (therefore only for Android), otherwise, for example as in the proposed case, we might as well use our object created in the B4XMainPage directly, it is useless to assign then it to GlobalContext.

Right?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Thank you. So, not surprisingly, it is used like any other "generic" object.
This is my second day after vacations so I'm still a bit slow..
 
Upvote 0

udg

Expert
Licensed User
Longtime User
@LucaMs : in B4J you will instatiate your "common class" and assign it to GloabContext in Main.AppStart (if I'm not wrong)

Anyway, if we consider the use case outlined by Erel (a push notification that "wakes up" our app), the question becomes why using GlobalContext instead of the global var defined in B4XMainPage as we do in a case like the one I described in my first post?
In other words, which is the usefulness of GlobalContext ?
 
Upvote 0
Top