Android Tutorial Starter Service - Consistent & Single Entry Point

Status
Not open for further replies.

bgsoft

Well-Known Member
Licensed User
Thank you very much Erel
 

luke2012

Well-Known Member
Licensed User
Hi @Erel. Which is the correct way to progammatically restart the Starter from another module (ex. Main module) when the App is already running?

Thanks in advance for your reply!
 

luke2012

Well-Known Member
Licensed User
Why do you need to restart it? Don't stop it and it will always run (while your process is alive).
@Erel you are right :) I'm looking for a better solution to my problem.
The problem is that I Init a net socket connection (net lib ---> https://goo.gl/JFbTAW) within the starter service.

All the connection parameters (IP and port) are passed reading from the realted preference manager fields (this to let user customize the connection).
So if the user change this parameters I need to reconnect the socket connection using this new putted paremeters (theorically the simple way is to restart the starter).

But probably isn't the most correct and optimized way.

Probably the correct solution is to call the "Connect" method form the module (ex. Main) in order to make a new connection using the new parameters:

B4X:
Starter.so.Close
Starter.so.Connect (host, port, 0)
Am I right?
 

Rusty

Well-Known Member
Licensed User
The main problem I see, as Dave O observed in post #27, is that ALL public variables declared in starter must be prefixed with the word "Starter." even within MAIN.

B4X:
' STARTER SERVICE
Sub Process_Globals
    Dim TestVar As String
End Sub
B4X:
'MAIN ACTIVITY (or anywhere the Starter variables are used)
Sub Activity_Create(FirstTime As Boolean)
    Starter.testvar
End Sub
This means that where MAIN has always been assumed (within MAIN) there is no "qualification" and the variable, if declared in MAIN, would be accessed as

B4X:
testvar = "x"

'if declared within starter, this code would be (within MAIN and all modules and services)
Starter.testvar = "x"
It's too bad that the public variables within Starter couldn't be used as the ones declared within MAIN. (I know that within other activities and services the variable would be used by MAIN.testvar = "x", but this has been inherent in the development from the beginning)

To me, this renders this less useful, but maybe I'm lazy too:)
Rusty
 

RVP

Active Member
Licensed User
So I rewrote an app to use the starter service, however now if I swipe the app away, and then restart it, the Main Activity is not displayed,

The log shows this:

B4X:
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
How do I get my Main Activity to redisplay when I restart the App?

Never mind. I had the Loadlayout being done inside a if FirstTime then .. End If, but turns out that isn't required. I have been doing this since day one, as I am sure I saw it shown that way in an example at some point and thought it was required to only load it once.

Guess not.
 
Last edited:

RjCl

Member
Licensed User
No one has yet said how to make the starter service ?

Is it Project -> Add New Module -> Service Module, and name it Starter ?

If not how ?
 
Last edited:

klaus

Expert
Licensed User
When you define a new project the Starter Service is already in the project.
If you want to add it to an 'old' project you can add it as you describe in your post.
 

DonManfred

Expert
Licensed User
No one has yet said how to make the starter service ?
Read post #1 of this thread

Notes

  • 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.
 

Dave O

Active Member
Licensed User
No, but I get around this by putting all global variables into a code module called "c" (for "common"). At least that cuts down some of the clutter to "c.xxxxx".
 

LucaMs

Expert
Licensed User
If the os kills your app while it is in the background then it can resume from the last activity.
This would really be a huge idiocy of Android (it is how Android "works", right, not a b4A "bug")!
Are you sure?
If so, I don't see other way to handle this case that ALLWAYS start Main directly from Service_Start... or not? And if I do so, might Main run twice (will two calls added to a queue?)
 
Last edited:

Erel

Administrator
Staff member
Licensed User
Are you sure?
Yes.

I don't think that it is a huge idiocy of Android. It is a very logical behavior. If the user switches to a different app, maybe to copy some text related to your app, and quickly returns to your app then the user expects to return to the same point. It is possible that the OS will kill the process while the app is in the background.

You can kill all activities when they are paused and the app will start from the main one.
 

LucaMs

Expert
Licensed User
If the user switches to a different app, maybe to copy some text related to your app, and quickly returns to your app then the user expects to return to the same point.
This is logical. It is what you do on Windows, using more than one sw at the same time. But if you "kill" (close) one of this sw and open again it, it will not start "randomly", but from the entry point, from the beginning (yes, I know, you created the Starter Service for this reason but I think this is a workaround of a big defect of Android.


You can kill all activities when they are paused and the app will start from the main one.
This solution, used only for rare cases where the app is killed by the operating system, may have contraindications (Activities will start from Activity_Create and not from Resume).

I don't think that it is a huge idiocy of Android.
Me too; I think it is more than huge :):mad:
 
Last edited:

LucaMs

Expert
Licensed User
You can kill all activities when they are paused and the app will start from the main one.
If so, I don't see other way to handle this case that ALLWAYS start Main directly from Service_Start... or not? And if I do so, might Main run twice (will two calls added to a queue?)
Could this last "mehod" create problems (related to queued calls, adding StartActivity(Main) as last statement in Service_Start)?
 

LucaMs

Expert
Licensed User
Another doubt. If this is Android's behavior, how did you make Starter always run first in any situation, even after the system killed the app when it was in background?

The authors of the Android could do the same thing, so that Main is always started first.

Or maybe you can do it.
 

Erel

Administrator
Staff member
Licensed User
If this is Android's behavior, how did you make Starter always run first in any situation, even after the system killed the app when it was in background?
The starter service feature is quite complicated. Under the hood the target activity or service start the starter service and make sure that Service_Create of the starter service is called before the target module code starts.

The authors of the Android could do the same thing, so that Main is always started first.
Or maybe you can do it.
B4A will not change the default Android behavior. If you want to do it in your app then there are all kinds of ways to do it. For further discussion you should start a new thread.
 
Status
Not open for further replies.
Top