Android Question B4X - Sharing Subs

luke2012

Well-Known Member
Licensed User
Longtime User
Hi all,
I'm new to B4X and I'm starting a new cross-platform development (B4A & B4i).
My question is: "which is the best way to share a sub between B4X components like B4XPages (https://bit.ly/3hgIPGp) from @Erel, Classes and modules ?
My target is to implement a set of sub (like a little framework) that can be called from anywhere within the App (Android or iOS). Starter is the answer ?

Thanks in advance for your precious help :)
Luca.
 

LucaMs

Expert
Licensed User
Longtime User
Exists a Starter service in b4i? I don't think.

I think you should create a class and an instance of if in the B4XMainPage.

From the tutorial:
There must be one class named B4XMainPage. This page will usually be the first one to be displayed.
I'm "reading" the tutorial and found:
13. There is a field named B4XPages.B4XGlobalContext. The use case behind it is with a class instance that is initialized in Service_Create of the starter service. This can be a good place to implement tasks that can also run in the background.
(there is a typo, its name is Globalcontext, without "B4X").

This also (?) could be the right place for your custom object.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Starter service is not the correct place for such code. Implement it in a class.
Yes, Erel, that's what I suggested in the first answer, but where to instantiate the class?

In that answer I suggested, looking at your tutorial, in the B4XMainPage but then I noticed that this does not necessarily have to be the first to show:
This page will usually be the first one to be displayed.

and that this "property" exists:
13. There is a field named B4XPages.B4XGlobalContext. The use case behind it is with a class instance that is initialized in Service_Create of the starter service. This can be a good place to implement tasks that can also run in the background.
which seems to be made just for this purpose.

So where to instantiate a global class, an "utility singletone", which will certainly be instantiated "immediately" and accessible from anywhere? B4XMainPage or B4XPages.GlobalContext ("here" you cannot instantiate your class, you could only assign your object to it. After this last sentence of mine, I think the answer is obvious: B4XMainPage).
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I noticed that this does not necessarily have to be the first to show:
The only case where it will not be the first page to show is when the developer decides to show a different page in the Initialize sub of B4XMainPage.
Still, B4XMainPage.Initialize will run before anything else (in B4XPages code).

It only makes sense to use GlobalContext, if your app can start in the background, without showing anything. In that case B4XMainPage will not run at all.
 
Upvote 0
Top