Android Question Regarding not being able to use MsgBox from Starter

Sandman

Expert
Licensed User
Longtime User
We all know that we can't show UI things from Starter (or any services really), like MsgBox etc. (Toasts are an exception for some reason, but for this thread I'll ignore them.)

Let's say I have a class in Starter. Then that class won't be able to use MsgBox etc. And if that class has a class in it, that in turn has a class in it, then that can't show MsgBox etc. You know, it's not possible, because they all inherit the Starter context.


So my question is this:

Is there a way for a sub to detect if it's in a Starter (or other service) context and thus cannot show MsgBox etc?
 

LucaMs

Expert
Licensed User
Longtime User
Is there a way for a sub to detect if it's in a Starter (or other service) context and thus cannot show MsgBox etc?
I don't know if that's possible (perhaps using Reflector) but... at that point what would your class do if it could realize it couldn't display the message, wouldn't it?

I would avoid using the Starter and other services as well; indeed, I just avoid doing it.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I would avoid using the Starter and other services as well; indeed, I just avoid doing it.
It's not really the topic of the thread, but I'll bite. In my case my app can wake up because a geofence event happened so the OS start one of my services. At that point I can only count on two things being alive: The service started, and Starter.

So in my case it makes sense to put important classes Starter so they are always available.

Opinions on this are welcome. ?


what would your class do if it could realize it couldn't display the message
I don't know really, I'm just trying to figure out if it's even possible at the moment.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Is there a way for a sub to detect if it's in a Starter (or other service) context and thus cannot show MsgBox etc?
Easiest way is to pass a flag as a parameter to the Sub as the programmer will know if it is being called in a service or not.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It's not really the topic of the thread, but I'll bite. In my case my app can wake up because a geofence event happened so the OS start one of my services. At that point I can only count on two things being alive: The service started, and Starter.

So in my case it makes sense to put important classes Starter so they are always available.

Opinions on this are welcome. ?



I don't know really, I'm just trying to figure out if it's even possible at the moment.
  1. use the B4XPages, which very often solve this type of problem.
  2. put the instances of the classes in the B4XMainPage or, if you don't want to use them, in the Activity Main (public variables) and in the Starter put only the calls to the methods of those objects.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Easiest way is to pass a flag as a parameter to the Sub as the programmer will know if it is being called in a service or not.
Yeah, sure, but is there a way to not have to do it like that and instead do some magic with JavaObjects or something like that?
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
use the B4XPages, which very often solve this type of problem
How on earth would B4XPages solve this? Like I wrote, when the OS wakes my service, only that service and Starter is alive. At no point during that is B4XPages initialized.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Also relevant, in this thread...

...Erel says
”the Starter service is the best place to initialize all the application-wide resources”
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I suppose you mean the part below?
2 - put the instances of the classes in the B4XMainPage or, if you don't want to use them, in the Activity Main (public variables) and in the Starter put only the calls to the methods of those objects.


I mean: put only the bare minimum in the Starter.



I have to ask, I can't remember the details about Main at the moment, but does Main always exist, as Starter does? Even when an app is launched like I've described, as the OS starts a service?
Yes, it exists immediately after starting the Starter. Using B4XPages, it will never pause, this is a great opportunity.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Also relevant, in this thread...

...Erel says
”the Starter service is the best place to initialize all the application-wide resources”
Yes but that only applies to B4A projects, of course. I prefer to initialize everything in the B4XMainPage, so if at a later time I want to create the iOS app, most of the work will be ready.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
This may be too obvious, but I tested this and it works. When called from Main it shows message if called from Starter it does not.
In you class wrap the msg function in a try block.

B4X:
Public Sub displayMsg(s As String)
    Try
        xui.MsgboxAsync(s, "Test Title")
    Catch
        Log(LastException)
    End Try
End Sub
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Now I am confused too. If it works it is called from an Activity context, if not then it is called from a Service.
I suppose that it isn't a msgbox per se you want to detect, just whether or not a sub is called from an Activity context or not.

Are there non-msg functions that depend on the Activity context? Ones that don't have a UI result? If so put one of these in the Try block.
There may be. Right now I can't think of any.
 
Upvote 0
Top