Android Question Activity vs Service

alizeti

Member
Licensed User
Is there a way that a service module as access to interface objects?

Example : Interface has a CustomListView and it's the service module that needs to fill that list.

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
No. Services can not Access UI Elements.
 
Upvote 0

emexes

Expert
Licensed User
But the other direction works: ie, UI module (aka Activity, eg: the default Main.b4a) can access service elements.

Here's one I just did an hour ago:
B4X:
NumAdsLabel.Text = Starter.NumAds
DeviceIdLabel.Text = Starter.DeviceId
DeviceNameLabel.Text = Starter.DeviceName
DeviceRSSILabel.Text = NumberFormat2(Starter.DeviceRSSI, 1, 1, 1, False)
 
Upvote 0

emexes

Expert
Licensed User
or if you zoom out a bit (and squint a lot) then you'll see what I often do - set a flag in the data-provider module (service eg Starter) to tell the display module (activity eg Main) that hey, something's changed, you better refresh that screen of yours which the display module picks up on in its next regular timer tick:

B4X:
sub Process_Globals
    Dim RefreshTimer As Timer
...

Sub Activity_Create(FirstTime As Boolean)
    RefreshTimer.Initialize("RefreshTimer", 200)
...
 
Sub Activity_Resume
    RefreshTimer.Enabled = True
...
  
Sub Activity_Pause (UserClosed As Boolean)
    RefreshTimer.Enabled = False
...
  
Sub RefreshTimer_Tick
    If Starter.RefreshFlag Then
        NumAdsLabel.Text = Starter.NumAds
        DeviceIdLabel.Text = Starter.DeviceId
        DeviceNameLabel.Text = Starter.DeviceName
        DeviceRSSILabel.Text = NumberFormat2(Starter.DeviceRSSI, 1, 1, 1, False)
        Starter.RefreshFlag = False
    End If 
End Sub
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Another way, instead of using a timer, is to use CallSub(Main, "RefreshScreen") after display variable(s) are changed, but I've found that usually ends up doing either:

(1) too many refreshes, and the device bogs down under the CPU load, or
(2) too few refreshes, because you've been trying to fix problem 1, and now the screen still seems sluggish but for the opposite reason ;-/

My cheats-way-out of refreshing the screen regularly at ~5 Hz doesn't work for all use-cases though. But, for appropriate situations, it works super!!!

And for frequently-changing stuff like counters and statuses, then it both lowers the CPU load and makes the display more readable.
 
Last edited:
Upvote 0
Top