Android Question CallSubDelayed to open an Activity

LucaMs

Expert
Licensed User
Longtime User
I was convinced that by calling from a service a routine of an Activity currently not in Foreground, this Activity would be open.

This happen-works only when a different Activity of the app is in Foreground (the app itself is in Foreground); if the app is paused, the call is added to a queue but then it is not executed when the app is bringed in Foreground (except, probably, if the target Activity is already visible).

Is it so?

Has it always been so?


*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (srvservice) Create ***
** Service (srvservice) Start **
** Activity (main) Pause, UserClosed = false **
Calling a routine of act2
sending message to waiting queue of uninitialized activity (routine)
** Activity (main) Resume **

srvservice uses CallSubDelayed to call a routine of act2, which is never executed.

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (srvservice) Create ***
** Service (srvservice) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (act2) Create, isFirst = true **
** Activity (act2) Resume **
** Activity (act2) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Calling a routine of act2
sending message to waiting queue (CallSubDelayed - Routine)
** Activity (main) Resume **

srvservice uses CallSubDelayed to call a routine of act2, which is never executed.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
I will read it again... maybe for the 5th time
- If the target module is an activity:
If the application is visible (one of its activities is visible) then the target module will be started if needed and the sub will be called.

but so, in this case, the queue does nothing (and the call will be removed from it), or am I wrong?
It depends on whether the OS kills the process or not. If not then the queues subs will be called when the activity is resumed.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It depends on whether the OS kills the process or not. If not then the queues subs will be called when the activity is resumed.
but not if a different activity is resumed. If I have the Main in foreground, I "pause" the app, a service calls "sub xxx" of a second activity, when I return to my app, I will see the Main again and the call queued...? Removed, correct?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
[I hope I do not seem insistent and nagging like... I actually am :D]

There is of course a good reason behind it. You should almost never start an activity while your app is in the background. The user doesn't expect it.

I think to cases like this:

1) my app is in foreground, Main activity;
2) I answer to a phone call or anyway I open a different app;
3) meanwhile a service of my app receives a msg to send to a second activity of my app;
4) I return to my app and now I would like that the second activity will be bring on foreground, to show the msg; instead the Main will be displayed and the message will not be displayed (of course I would find the way to save the message and view it later, but this is just an example that has nothing to do with my projects)
 
Upvote 0
Top