Android Tutorial Starter Service - Consistent & Single Entry Point

Status
Not open for further replies.

Roycefer

Well-Known Member
Licensed User
I thought you said the List access occurs in the Starter service. Why is the error occurring in Activity_Create?
 

JNG

Member
Licensed User
Hi Erel,

I am accessing data from remote server. Some of the part of the data I don't need upfront it is required on demand basis. To avoid memory leakage I want to access the same as an when require. Can I call starter service again in this case to do the job.

thanks
 

Dave O

Active Member
Licensed User
In Erel's recent "tips for B4X developers" post (which motivated me to clean up my code), he states that "Public (process_global) variables should be declared and set / initialized in Service_Create of the Starter service".

I understand that it's best to initialize/assign values to those public variables in Starter > Service_Create, because it's the only guaranteed entry point.

However, does it really matter where we declare our Process_Global variables - in Starter, or a common code module, or an activity?

There are a few reasons I ask this:
- I typically put PG variables in a common code module called "c", so the prefix is kept short when referenced from other modules (e.g. c.someVariable instead of Starter.someVariable). It reduces clutter a bit.
- If a PG variable is mostly used in a single module (and rarely referenced outside it), I usually put the variable in that module to keep it "close to home" (and to avoid prefixing in most cases).
- I'm hoping to minimize the amount of code I need to move around, probably because I'm lazy. :)

Thanks!
 

ArminKH

Well-Known Member
In Erel's recent "tips for B4X developers" post (which motivated me to clean up my code), he states that "Public (process_global) variables should be declared and set / initialized in Service_Create of the Starter service".

I understand that it's best to initialize/assign values to those public variables in Starter > Service_Create, because it's the only guaranteed entry point.

However, does it really matter where we declare our Process_Global variables - in Starter, or a common code module, or an activity?

There are a few reasons I ask this:
- I typically put PG variables in a common code module called "c", so the prefix is kept short when referenced from other modules (e.g. c.someVariable instead of Starter.someVariable). It reduces clutter a bit.
- If a PG variable is mostly used in a single module (and rarely referenced outside it), I usually put the variable in that module to keep it "close to home" (and to avoid prefixing in most cases).
- I'm hoping to minimize the amount of code I need to move around, probably because I'm lazy. :)

Thanks!
as the starter service will never killed as long as your app is running so is the best point for initializes or hold resources(Global resources)
otherwise some time activity or code module will be closed by OS or mistakenly by yourself so your app will be crashed
imagine you have a service which started by ServiceStartAt at background and uses an sql object or an image which is declared and initialized in Code Module
now can your service get access to that sql object or image if your app is closed!!!?
as @Erel said
"The Starter service will be created and started and only then the activity or service that were supposed to be started will start."
so any time your activity or service or code/class modules need to any resources then all of them has a trusted point to access wide resources
Concise writing is not always good
 
Last edited:

Dave O

Active Member
Licensed User
imagine you have a service which started by ServiceStartAt at background and uses an sql object or an image which is declared and initialized in Code Module. now can your service get access to that sql object or image if your app is closed!!!?
That's basically my question right there - if I declare my image in a code module, and initialize it in the Starter>Service_Create, is that OK?
I'm assuming that when the Starter service is called, all the Process_Globals variables are declared first (regardless of module), in which case it shouldn't matter where each variable is declared (as long as it's PG). Is that correct?
 

klaus

Expert
Licensed User
The Program flow at start is:

Main.Process_Globals
Starter.Process_Globals
Other Activity.Process_Globals
Other CodeModule.Process_Globals
Starter.Service_Create
Starter.Service:Start
Main.Globals
Main.Activity_Create
Main.Activity_Resume
 

Dave O

Active Member
Licensed User
@klaus , thanks for that.

And I assume that the last 3 items (Main module) might be replaced by another activity in those edge cases that Erel mentioned?
 

Mahares

Well Known Member
Licensed User
Main.Process_Globals
Starter.Process_Globals
@klaus: i always thought the order should be the below, since the starter service is the initial entry point of any application that has it:
Starter.Process_Globals
Starter.Service_Create
Starter.Service:Start
Main.Process_Globals
I was basing my assumption based on this Erel's statement in his Starter tutorial:
The Starter service will be created and started and only then the activity or service that were supposed to be started will start.
Could you please enlighten us.
 

PSEAD

Member
Licensed User
I am busy rewriting some old code to clean it up, and I just want to confirm, given the results of "programflow": Am I correct in assuming that Process_Globals declared in code modules would therefore be treated the same as if they were declared in the starter process, and would be fine as long as the service is active?
 

Erel

Administrator
Staff member
Licensed User
Am I correct in assuming that Process_Globals declared in code modules would therefore be treated the same as if they were declared in the starter process, and would be fine as long as the service is active?
That's correct (they will also be "fine" even if the service is no longer active).

As a general recommendation all public process globals should be declared in Starter service and initialized in Service_Create.
 

PSEAD

Member
Licensed User
Thanks. Good news. It just becomes a problem declaring them in the starter service when using common code modules used by a few different apps.
 

PSEAD

Member
Licensed User
It looks like the stater service continues to run after the main activity is closed. (In my case this is good because I am using a kiosk service).
Under what conditions, besides stopping the service in code, would the service stop running?
Should we kill the starter service in the activity_pause event?
 

lemonisdead

Well-Known Member
Licensed User
Under what conditions, besides stopping the service in code, would the service stop running?
Hello,
The system will kill the service when it needs more resources. But I guess it could be put as Sticky like any other services (https://www.b4x.com/android/forum/t...-long-running-background-tasks.27065/#content) ~to be confirmed ~
Should we kill the starter service in the activity_pause event?
In fact it is a "service" so you should or shouldn't stop it depending on what you need to be done. It's your choice.
 
Status
Not open for further replies.
Top