Service module and global vars?

marcel

Active Member
Licensed User
Longtime User
Hi,

I have a question on the behaviours of activities and services. When I startup the program the first activity will be started and this start a services. The services starts it selfs every 5 minutes.
When the user clicks a new program. The activity moves to the background and can be killed by the OS. The service keep running (normaly) but this one can also be killed I suppose by the os. In my logging I will see the following:

Service destroy
Service create.

I assume that the OS have destroid the service for some reason. What will happen to my global vars in this situation?
I have a SQL variable in my Main activity and this will be initializid in the first start of the program. I use this var also in the service but I see when the Service is destroyed and restart I get an error that the SQL is not initialized.

What is the prefered way of working.... Do I need a SQL var seperatly on every activity and service? Or just on the service and the activities can use the var?

I am a kind of Lost here....
 

mc73

Well-Known Member
Licensed User
Longtime User
Though it's usually recommended to have one single sql object, I tend to initialize two different instances both in the service and the activity. I cannot risk initializations' errors when things like the one you've just described show up :)
 
Upvote 0

marcel

Active Member
Licensed User
Longtime User
What will happen with those global vars. For example i am also using a function that create the path voor de database and store this in a global var. So this infomration is all lost? Can i see the service a a seperate process that runs on it self and does not interact (normally) with other activities?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
sure you can. keep your service in foreground and your variables should be safe.
 
Upvote 0

marcel

Active Member
Licensed User
Longtime User
use service.startforeground :)

Ok. but this keeps the services continue running which drains the battery?

I am now using StartServiceAt...

But if a global var in my main activity is killed for some reason can I do this in my service:

B4X:
   If Not(Main.items.IsInitialized) Then
     Main.items.Initialize
   End If
 
Upvote 0
Top