Android Question StartActivity(Main) is equal to CallSubDelayed(Main, "show_Activity_1")

D

Deleted member 103

Guest
Hi,

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
 

Attachments

  • TwoActivities.zip
    7.5 KB · Views: 142
D

Deleted member 103

Guest
Not really, they appear to be the same but they are not.

Read THIS
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.
in this case is activity-1 paused, then should CallSubDelayed wait until the activity-1 is ready, or did I not understand something?
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
Why not pass the result as a parameter?
B4X:
CallSubDelayed2(Main, "ItemClicked",Value)
but there is still a hard-coded "Main", its not good if you will reuse this select list.
 
Upvote 0
D

Deleted member 103

Guest
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.

I hope I have described it understandable.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Recently I am more "smart" than usually, as you know :D

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.
 
Last edited:
Upvote 0
D

Deleted member 103

Guest
Recently I am more "smart" than usually, as you know :D

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.
I've changed the routine now, I've exchanged CallSubDelayed with CallSub, and now everything's OK again.
 
Upvote 0
Top