Android Tutorial Starter Service - Consistent & Single Entry Point

Discussion in 'Tutorials & Examples' started by Erel, Aug 25, 2015.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    One of the challenges that developers of any non-small Android app need to deal with, is the multiple possible entry points.

    During development in almost all cases the application will start from the Main activity.
    Many programs start with code similar to:
    Sub Activity_Create (FirstTime As Boolean)
    If FirstTime Then
      SomeBitmap = 
    '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 those 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.

    Starting from B4A v5.20 there is a new feature named Starter service that provides a single and consistent entry point. If the Starter service exists then the process will always start from this service.

    The Starter service will be created and started 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.
    The Starter service should be the default location for all the public process global variables. SQL objects, data read from files and bitmaps used by multiple activities should all be initialized in the Service_Create sub of the Starter service.


    • The Starter service is identified by its name. You can add a new service named Starter to an existing project and it will be the program entry point.
    • This is an optional feature. You can remove the Starter service.
    • You can call StopService(Me) in Service_Start if you don't want the service to keep on running. However this means that the service will not be able to handle events (for example you will not be able to use the asynchronous SQL methods).
    • The starter service should be excluded from compiled libraries. Its #ExcludeFromLibrary attribute is set to True by default.
    • The starter service should never be explicitly started. This means that if you want to start a service at boot then add a different service.
    Android 8+ considerations
    • Starting from Android 8, the OS kills services while the app is in the foreground.
    • If you are using B4A v8.30+ then the starter service will not be killed until the whole process is killed.
    • You will see a message such as this one, 60 seconds after the app has moved to the background:
      ** Service (starter) Destroy (ignored)**
    • The above message means that the actual service is destroyed but the starter service is still available.
    • Don't make the starter service a foreground service. It will not work once the backed service is destroyed.
    • If you want to do a background task then you should add another service.
    • Related tutorial: Automatic Foreground Mode
    Last edited: Sep 20, 2018
  2. Luis Felipe

    Luis Felipe Member Licensed User

    Awesome ! thank you Erel. Every day improving B4A ...
  3. ArminKH

    ArminKH Well-Known Member

    the starter service will be always running? or it is starts when each service module starts?
  4. Beja

    Beja Expert Licensed User

    Great development indeed.. thanks Erel.
  5. Erel

    Erel Administrator Staff Member Licensed User

    The starter service will start running when the process is created. It will not be started again when other services are started.

    If you like you can stop it (see the notes in the first post).
  6. TheMightySwe

    TheMightySwe Active Member Licensed User

    Is it possible to read intents in it, or is that done in Main as before?
  7. Erel

    Erel Administrator Staff Member Licensed User

    Intents should be read in the activity or service that are started after the Starter service.
    lemonisdead likes this.
  8. aaronk

    aaronk Well-Known Member Licensed User

    At the minute I am using a service and in that service I am initializing a class and it's being called from multiple Activity's.

    Does this mean I should move all functions from my current service and move it to the Starter service module ?
  9. grafsoft

    grafsoft Well-Known Member Licensed User

    Great! I could eliminate code like "if main.sql1.isinitialized=false then ..."
    Erel likes this.
  10. Erel

    Erel Administrator Staff Member Licensed User

    If your current code works properly then you can keep it. The solution might be simpler if you remove that service and move all the code to the starter service.
    lemonisdead likes this.
  11. b4auser1

    b4auser1 Well-Known Member Licensed User

    Is it possible to use service_destroy event in the starter service as an exit point where to release all common application resources (sql.close, ...) ?
  12. Erel

    Erel Administrator Staff Member Licensed User

    Not really. However you do not need to close SQL. The file handle will be closed automatically when the process is killed.

    Service_Destroy is not called when the OS or the user kills the process.
  13. PhilipBrown

    PhilipBrown Active Member Licensed User

    Isn't it always the Main activity which runs right after the Starter service ends?
  14. Erel

    Erel Administrator Staff Member Licensed User

    No. If the os kills your app while it is in the background then it can resume from the last activity. There are other cases as well.
    lemonisdead and ArminKH like this.
  15. ArminKH

    ArminKH Well-Known Member

    Erel starter service is always running as long as os dont kill that ,so is'n this mean maybe this is a point which has higher cpu and battery usage than normal?
    I know this service started once but is always running and any process has some usages of cpu and battery
    bryantm1122 likes this.
  16. Erel

    Erel Administrator Staff Member Licensed User

    If your app is in the background then Android will kill it eventually. It is the OS responsibility.

    However you can stop the service if you don't need it by calling StopService(Me) in Service_Start sub.
    bryantm1122 and ArminKH like this.
  17. ArminKH

    ArminKH Well-Known Member

    No,my goal is not life cycle
    I want to know how much is the usage(cpu and battery) of service when is always running with some light weight resources
    I want to compare cpu and battery usage when we define resources into activity with when we define same resources into Starter service
    Which one has higher usage?Activity or Service?
  18. Erel

    Erel Administrator Staff Member Licensed User

    They don't consume any CPU cycles unless there is some code running.
  19. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    I got "java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0" when trying to access a list inside Starter service after screen lock .
  20. Roycefer

    Roycefer Well-Known Member Licensed User

    Where was that List declared?
Thread Status:
Not open for further replies.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice