Android Question Public_Globals and Starter

udg

Expert
Licensed User
Longtime User
Hi all,

since the introduction of the Starter module we were encouraged to use it as the place where to define and initialize our global vars.
A good reason is that it's not a safe assumption to count on Main to be executed as the first Activity when the OS runs our app.

Now, let's say I'd like to keep a Public var in the Activity where it (logically) belongs to. Think of it as the initial data used by the called Activity, where data is initialized and prepared by the calling one.
Things should go something like this:
B4X:
'Activity 1
...
define a "parameters" map/object/var and initialize it
put items in the map according to what Activity2 expects
Activity2.myvar = "parameters" map
StartActivity2

'Activity2
uses myvar to set up what's needed here

My question is: will it be safe to simply check for the initialization of myvar in Activity2.Resume and eventually initialize it to default or empty values?
Or is it better to stay with the golden rule "every public global var in the Starter"? TIA
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you are using this map to pass data between activities then you can simplify your code by using CallSubDelayed2. Just pass the map when you call a sub in the second activity.

will it be safe to simply check for the initialization of myvar in Activity2.Resume and eventually initialize it to default or empty values?
Yes. Just make sure that you don't assume that Main code ran before Activity2 code.

Another option is to initialize this map in Service_Create of the Starter service:
B4X:
Sub Service_Create
 Activity2.myval = CreateMap(<default values here>)
End Sub
 
  • Like
Reactions: udg
Upvote 0

udg

Expert
Licensed User
Longtime User
Thank you. My question was an hypothetical one, just to be sure to have a correct knowledge about how things work.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Most likely I've read too fast (and I'm too fast writing this post :)) but just checking that the map is initialized may not be enough, I think, because if the Activity is called multiple times, the map could be initialized but the the values in it may not be up to date.

But I'll have to read these posts better :)
 
Upvote 0
Top