Until Basic4android v2.00 the way to pass information between activities and services was through process global variables.
CallSubDelayed makes it much simpler. It allows you to call a sub in a different service or activity. If the target module is not active, then it will be started automatically. When the target module is ready, the sub will be called.
CallSubDelayed doesn't immediately call the target sub. It sends a message to the message queue. The internal framework manages this message and passes it to the target module when it is ready.
CallSubDelayed can also be used to call subs in the current module. It is useful in cases where you want to run some code "right after" the execution of some UI event.
Rules
- If the target module is already running then the sub will be called.
- If the target module is a service and it is not already running then it will first be started (Service_Create and Service_Start will first be executed).
The sub will be called after Service_Start.
- If the target module is an activity:
Just to make it clear, you do not need to call StartActivity or StartService when you use CallSubDelayed.
CallSubDelayed is the recommended method for interaction between activities and services.
Note that you cannot use CallSubDelayed (or CallSub) with code modules.
CallSubDelayed can be used with class instances. However the containing module will not be started if it is not already running.
An improved version of the two activities example:
CallSubDelayed makes it much simpler. It allows you to call a sub in a different service or activity. If the target module is not active, then it will be started automatically. When the target module is ready, the sub will be called.
CallSubDelayed doesn't immediately call the target sub. It sends a message to the message queue. The internal framework manages this message and passes it to the target module when it is ready.
CallSubDelayed can also be used to call subs in the current module. It is useful in cases where you want to run some code "right after" the execution of some UI event.
Rules
- If the target module is already running then the sub will be called.
- If the target module is a service and it is not already running then it will first be started (Service_Create and Service_Start will first be executed).
The sub will be called after Service_Start.
- 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.
- If the application is in the background (can happen when a service calls an activity) then the message will be stored in a special message queue. In this case the sub will be called when the target activity becomes visible. The sub will be called before Activity_Resume.
- If the application is in the background (can happen when a service calls an activity) then the message will be stored in a special message queue. In this case the sub will be called when the target activity becomes visible. The sub will be called before Activity_Resume.
Just to make it clear, you do not need to call StartActivity or StartService when you use CallSubDelayed.
CallSubDelayed is the recommended method for interaction between activities and services.
Note that you cannot use CallSubDelayed (or CallSub) with code modules.
CallSubDelayed can be used with class instances. However the containing module will not be started if it is not already running.
An improved version of the two activities example:
B4X:
'Main Activity
Sub Process_Globals
End Sub
Sub Globals
Dim Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
End Sub
Sub Button1_Click
'This call will bring Activity2 to front and will then execute ShowList
CallSubDelayed2(Activity2, "ShowList", "This is the title")
End Sub
Sub GetResult(Result As String)
Label1.Text = "You have chosen: " & Result
End Sub
'*****************************
'Activity2
Sub Process_Globals
End Sub
Sub Globals
Dim ListView1 As ListView
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("2")
For i = 1 To 100
ListView1.AddSingleLine("Item #" & i)
Next
End Sub
Sub ShowList(Title As String)
Activity.Title = Title
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
'this call will bring Main to front and call GetResult
CallSubDelayed2(Main, "GetResult", Value)
End Sub