Android Question I'm confused - services and process global variables!

Graeme Tacon

Member
Licensed User
Longtime User
Relatively new to B4A, so excuse the, possibly daft, question, which is probably down to my complete misunderstanding!

The app I'm writing has a few activities and a service that get kicked off with a StartServiceAt command. All works fine, unless the process is shut down before the service start time. The service then dies with a null exception.

Looking at various posts on the site, I think the problem is that my service uses variables defined in the Main activity Process Globals.

What is the best way around this ? Would the same problem occur if I move those variables to the Code Module process globals ? Should I write the variables I need to storage (with kvs for example) in the activity and read them back in as part of the service ?

Should variables defined in Activity Process Globals only be used by other activities ?

Any help / advice appreciated.

Thanks
Graeme
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It doesn't matter where the process global variables are declared.

You can access variables from the Main module. However you should understand that the code in Activity_Create didn't run in this case as the activity was not created.

One solution is to use a code module for all the shared resources with an Init sub:
B4X:
Sub Init
 If InitHasAlreadyRun = True Then Return
 InitHasAlreadyRun = True
 SQL1.Initialize(...)
 ...
End Sub

Now you should call this sub (SharedModule.Init) from each of the starting points.
 
Upvote 0
Top