I do not know if it's a mistake, but I think the 2 functions should not have the same functionality.
It can not be that the function "CallSubDelayed (Main," show_Activity_1 ") has the same effect as "StartActivity (Main)", with both functions the main activity is started each time.
Activity 1:
B4X:
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 Activity_Resume
If Activity2.result.Length > 0 Then
Label1.Text = "You have chosen: " & Activity2.result
Else
Label1.Text = "Please press on the button and choose an item."
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub show_Activity_1
Log("show_Activity_1")
End Sub
Sub Button1_Click
StartActivity(Activity2)
End Sub
Activity 2:
B4X:
Sub Process_Globals
Dim result As String
result = ""
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 Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
result = Value 'store the value in the process global object.
'StartActivity(Main) 'show the main activity again
CallSubDelayed(Main, "show_Activity_1") 'show the main activity again ?
End Sub
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.
I'm not sure that I understand the question or problem. Your program seems to run fine.
Several mistakes:
- Not using anchors.
- targetSdkVersion is missing.
- Why not set the selected item in show_Activity_1?
- Why not pass the result as a parameter?
With this example I just wanted to show that the function "CallSubDelayed" can start an activity as well as "StartActivity (other_activity)".
I thought until now that the function waits until the other activity is ready.
But in my app this functionality causes a problem.
There are 2 activity and one class.
With this class I call again and again a procedure in Activity-1.
As long as Activity-1 is active, everything is ok, but if Activity-2 is active, then Activity-1 will be started without Activity-2 being terminated, and this will cause an endless loop.
Recently I am more "smart" than usually, as you know
Perhaps you're talking about a specific case, Filippo, otherwise I can see differences between the two methods to start Main (or another Activity).
Of course, if you start an Activity using CallSubDelayed you start a specific routine of that Activity which will be not executed if you simply use StartActivity.
Also, you may have other commands after CallSubDelayed that would be executed before the Activity is started.
Recently I am more "smart" than usually, as you know
Perhaps you're talking about a specific case, Filippo, otherwise I can see differences between the two methods to start Main (or another Activity).
Of course, if you start an Activity using CallSubDelayed you start a specific routine of that Activity which will be not executed if you simply use StartActivity.
Also, you may have other commands after CallSubDelayed that would be executed before the Activity is started.