Android Question CallSub vs CallSubDelayed

ValDog

Active Member
Licensed User
Longtime User
Can someone clearly explain when to use one or the other? If calling a sub in a paused activity should which should I use? If calling a sub from a service which should I use?

Thank in advance!
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
The problem is different : CallSubDelayed is a mix with StartActivity and Callsub
This means that you can use CallSub if you know that the activity or the service is not paused (not(IsPaused)).
If you use CallSubDelayed than the service or the activity will first be started if they are paused.
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
Which you use will depend on the result you want to achieve. For example, I fetch a user's profile and and request their pics; all the interaction with the server is done in a service module that handles the API call, but the profile display is done by an activity. A profile might be requested from somewhere else, so it's appropriate for the service to use CallSubDelayed to start the profile viewer activity.

But, a user might be bored waiting for all the pics, or decide they don't like the look of what they've seen, and press Back before they've all loaded. So if it's a photo that's arrived, the service uses IsPaused to check if the activity is running; if it is, it uses CallSub to display to picture, otherwise it ignores it. If CallSubDelayed was used, the profile viewer would pop up again, which would be a bit annoying.

So, bear in mind both the difference explained by lemonisdead above and think about how your choice of one of the other might affect the experience of the user
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
The problem is different : CallSubDelayed is a mix with StartActivity and Callsub
This means that you can use CallSub if you know that the activity or the service is not paused (not(IsPaused)).
If you use CallSubDelayed than the service or the activity will first be started if they are paused.
Another important difference to consider is that CallSubDelayed runs your code after the current sub has ended (hence Delayed in its name). Technically, it posts a runnable in the message queue. It's very useful when you have events that do not allow calls to DoEvents or modal dialogs. In these cases, instead of handling the event directly in the event handler sub, you call another sub with CallSubDelayed and you place all your code in this sub, including DoEvents and MsgBox if needed. It's also useful to priorize tasks (for example when you want that a certain task is done right now in some cases, or only when the current treatment is finished).
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The problem is different : CallSubDelayed is a mix with StartActivity and Callsub
This means that you can use CallSub if you know that the activity or the service is not paused (not(IsPaused)).
If you use CallSubDelayed than the service or the activity will first be started if they are paused.


This also means that you can not call a routine of an activity from another activity using CallSub but you have to use CallSubDelayed
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
If you use CallSubDelayed than the service or the activity will first be started if they are paused.

Is the paused activity automatically started immediately ?

I have a paused activity (Main) .

From my service ..
B4X:
CallSubDelayed(Main,"Process_Results")
results in log .. sending message to waiting queue (CallSubDelayed - Process_Results)

It is not until I click service notification or manually restart app the sub "Process_Results .. is run.
Is this the norm .. or am I missing something. I need some method to restart paused app and call designated sub.

Would declaring a Timer in Main activity Process_Globals .. then pausing the activity, cause CallSubDelayed to be queued. ?

Thanks
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Probably not, The timer would continue to run, using battery at least. but the Tick sub would not get called while the activity is paused. I think it would be on a different thread to CallsubDelayed as I believe that uses a message queue.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Thanks Steve .. so there is something else (code within my app) that is stopping CallSubDelayed restarting my app immediately ??
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I think that Immediately is a relative term when dealing with multitasking systems. It is going to depend on what else the OS is dealing with. My understanding is that Callsub delayed will call the sub when the current processing is complete. but that may not just be the processing within your app, but maybe the processing that the OS is doing else where.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I think that Immediately is a relative term when dealing with multitasking systems. It is going to depend on what else the OS is dealing with.
That is my current understanding as well .. but surely the message would be processed within a minute or so ??

So if I want a paused app to be started ' Immediately ' to run a designated sub, I would be better off just setting a flag and calling StartActivity from with the service .. , with the resuming app testing for existence of flag to determine if the sub is to be called. ?

Thanks again
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Yes, I would have expected it to be called sooner rather than later, sounds like you have a better working knowledge of that scenario than me. I've only used services rarely. And from memory, they have worked as expected. i.e. calling subs in good time when paused.
 
Upvote 0
Top