The object of this exercise is to use SubDelayed2 in Activity2 to display a value in MAIN activity. But the value returned is blank. I can move the sub to Main and get the value I want, but that will defeat the purpose of the exercise.
Is there any way to modify my code and stick with SubDelayed2 to display the correct value?
Thank you
'MAIN activity:
B4X:
Sub Process_Globals
Dim MyTeam As String
End Sub
Sub Activity_Create(FirstTime As Boolean)
StartActivity(Activity2)
End Sub
Sub GetTeam(s As String) As String
MyTeam= "My favorite soccer team is located in " & s
End Sub
'Activity2:
B4X:
Sub Activity_Create(FirstTime As Boolean)
CallSubDelayed2(Main,"GetTeam","France")
Log("My Team is: " & Main.MyTeam) 'Displays a blank value, Why???
End Sub
Thanks TDS. Do you mean CallSub2(Main,"GetTeam","France") instead of using CallSub because I need to pass the parameter "France". I even tried callSub2 but it still shows blank.
The thing is you are trying to call one sub in another activity.
Thinking about the activity life cycle, code in another activity cannot be executed unless it is active. And two activities cannot be active at the same time.
Its not a matter of where the sub is 'placed'.
I think you should stick with callsubdelayed2 which will work, but move the log statement into Activity1 immediately after setting MyTeam. Obviously you won't need to prefix it with the 'main' once in this activity. I think it will then work.
Thanks again TDS and RandomCoder. Here is the final solution that finally worked. Putting this code in Activity2:
B4X:
Sub Activity_Create(FirstTime As Boolean)
If Not(IsPaused(Main)) Then
CallSub2(Main,"GetTeam","France") 'CallSubDelayed2 also works
Log("My Team is: " & Main.MyTeam)
End If
End Sub