Other [new feature] Starter Service

Status
Not open for further replies.

Erel

B4X founder
Staff member
Licensed User
Longtime User
One of the challenges that developers of any non-small Android app need to deal with, is the multiple possible entry points.
Updated tutorial is available here: https://www.b4x.com/android/forum/threads/starter-service-consistent-entry-point.57599/

During development in almost all cases the app will start from the Main activity.
I'm sure that many programs start with code similar to:
B4X:
Sub Activity_Create (FirstTime As Boolean)
If FirstTime Then
  SQL.Initialize(...)
  SomeBitmap = LoadBitmap(...)
  'additional code that loads application-wide resources
End If
End Sub
Everything seems to work fine during development. However the app "strangely" crashes from time to time on the end user device.
The reason for these crashes is that the OS can start the process from a different activity or service. For example if you use StartServiceAt and the OS kills the process while it is in the background.
Now the SQL object and the other resources will not be initialized.

The next version of B4A will include a new (optional) feature that will make it much simpler to manage this issue.
The process will always start from the Starter service. The Starter service will be created and only then the activity or service that were supposed to be started will start.
This means that the Starter service is the best place to initialize all the application-wide resources.
Other modules can safely access these resources.

This is an optional feature. You can safely remove the Starter service and you can also add this service to existing projects.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
The reason for these crashes is that the OS can start the process from a different activity or service. For example if you use StartServiceAt and the OS kills the process while it is in the background.

Does it mean that this can happen only in special cases, in which, as in the example, we use a service, and if the process REstarts? Or can it happen using the Main activity for the initialization normally? Can the OS kill the process at its first start?

The reason for these crashes is that the OS can start the process from a different activity or service. For example if you use StartServiceAt and the OS kills the process while it is in the background.

This seems related to a process REstart.


So, suggest you to use a service that way in every application even now (waiting for the new version)? (the answer should be obvious :))


Thank you
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It can also happen if there are multiple activities and the user presses on the home button. The OS can kill the process and later when the user returns to the app from the task manager the app will start from the last activity.

So, suggest you to use a service that way in every application even now (waiting for the new version)?
It is not so simple. I usually recommend using a static code module with an init sub. Though it is less powerful than the upcoming starter service.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
It can also happen if there are multiple activities and the user presses on the home button.
Wow, thank you Erel, i've always had this issue with my Pirate Bay app, of course i had to take the app down from the google store, but i can see this will be a great feature to have.

Awesome.
Walter
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It can also happen if there are multiple activities and the user presses on the home button. The OS can kill the process and later when the user returns to the app from the task manager the app will start from the last activity.

This sentence again confirms that you are talking about a REstart of the process, not of the first start of the app.

Thanks
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is relevant in many cases. For example you can set a service to start at boot. The process will start from the starter service and then the other service will be started.
You can add a static intent filter for incoming SMS messages. Again the starter service will start and then the service that is set in the manifest.
 
Upvote 0

Sgardy

Member
Licensed User
Longtime User
I have already a service named Starter... should I change something now?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I have already a service named Starter... should I change something now?
If you will not change the service name then the program will start from this service. Whether it is good or bad depends on your requirements.

The new starter service seems clever, but under what conditions does it's Service_Destroy sub fire?
If you don't explicitly stop the service then it will never fire. You can safely stop the service in Service_Start with StopService(Me) if you don't need to keep the service running (for example if you need to handle events of the service objects).
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
This may solve a problem I have with my app, if it quits and I rerun it, I get lots of errors, about things not being initialized. hopefully this will solve that problem for me! keep up the great work!
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
You can safely stop the service in Service_Start with StopService(Me) if you don't need to keep the service running (for example if you need to handle events of the service objects).

I still don't understand how it exactly works.

I currently have a static code module with an init sub which is called from every activity and every service in my app. I understand that I can move all this initialization stuff from my sub to Service_Start of the Starter service. From then on I don't need to care about any initializations in my activities or service modules.

Now I have some questions to clarify things.

1. Is the Starter service identified just by it's name? So I can add a service module called "Starter" and this will work?

2. If I just do some initializations of global objects in Service_Start is it safe to stop the service at the end of Service_Start? So will the Service start again when needed in this case?

3. The Starter service seems to be a good place for catching "global" events like network state changes, location events, physical activity detection etc. Is this true or should it only be used for initializations?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Better to move the code to Service_Create.
1. Yes.
2. As I wrote above it is better to use Service_Create. You can stop the service from Service_Start if you don't need to handle any events in the service. For example if you are using asynchronous SQL methods then you need to keep the service running.
I don't recommend you to restart the service again. This is not its purpose. Use a different service instead.

3. This is true (assuming that you haven't stopped it).
 
Upvote 0

Dave61

Member
Licensed User
Longtime User
I am a little confused by this new Starter service, especially since my application (that was working before I updated) no longer works - now it comes up with an error saying "Object should first be initialized (Activity)".

1) Do I HAVE to have a Starter service? Or should existing programs run without it, directly from the Main Activity Module?

2) Do I also still have to have an activity called Main? I was messing about and tried to rename the Main module but it won't let me. Interestingly it will still let me rename the Starter Service module.

3) Where in the project does one set the starting module for a project? i.e. how does it know whether to start from Starter or Main?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1) Do I HAVE to have a Starter service? Or should existing programs run without it, directly from the Main Activity Module?

2) Do I also still have to have an activity called Main? I was messing about and tried to rename the Main module but it won't let me. Interestingly it will still let me rename the Starter Service module.

3) Where in the project does one set the starting module for a project? i.e. how does it know whether to start from Starter or Main?
1. No. The program should behave exactly like in previous versions if there is no Starter service module.
2. Yes. You need to have a Main module. You can change the Starter service name. It will then act as a regular service.
3. If there is a service named Starter then it will be the program entry point. If there isn't such a service then the OS will decide which module is the entry point. By default it will be the main module.

I would be happy to see the project that worked in previous versions and now fails. If you can please send me the project to [email protected].
 
Last edited:
Upvote 0

Dave61

Member
Licensed User
Longtime User
Thanks for the clarifications guys. I understand a lot better now.

Unfortunately Erel I can't replicate the problem I had with the code initially as I messed around and got it working before I saw your email this morning. It may have been something else - I get some strange things happen when I have multiple copies of B4A running at the same time.

I should really brush up on getting B4A & github working together as I am used to with my .Net coding - then I would be able to go back & see what my problems really were instead of guessing! :)
 
Upvote 0

mr23

Active Member
Licensed User
Longtime User
When there is a Starter service, and there is also a Widget service, is the starter service still guaranteed to start before the widget service under all conditions?

Update: I did find this in another thread.
The starter service will start running when the process is created. It will not be started again when other services are started.
 
Last edited:
Upvote 0
Status
Not open for further replies.
Top