Android Question Why this SubExists don't works?

LucaMs

Expert
Licensed User
Longtime User
I have this code in the Main Activity:
B4X:
Sub Button1_Click
    'This call will bring Activity2 to front and will then execute ShowList
    If SubExists("Activity2", "ShowList") Then ' don't works
'    If SubExists(Activity2, "ShowList") Then ' don't works
        CallSubDelayed3("Activity2", "ShowList", "Main", "This is the title")
    End If
End Sub

SubExists returns False in both ways, with or without quote.

Activity2:
B4X:
Public Sub ShowList(Caller As String, Title As String)
    mCaller = Caller
    Activity.Title = Title
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
    'this call will bring Main to front and call GetResult
    If SubExists(mCaller, "GetResult") Then
        CallSubDelayed2(mCaller, "GetResult", Value)
    End If
End Sub

Here, SubExists finds GetResult.
 

Attachments

  • TwoActivities.zip
    7.4 KB · Views: 232

barx

Well-Known Member
Licensed User
Longtime User
try the sub name in lower case. Sometime b4a uses lower-case only, Not sure if it true in this case but worth a try.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
(I removed the post from the other thread. It is not relevant to that thread)

My previous answer was incorrect.

SubExists returns False because the second activity was not yet created.
CallSubDelayed does throw an error if the target sub doesn't exist. This is by design. You will also see a warning about it. This is the same as trying to directly call a sub that doesn't exist

If you want to implement an event then you should usually use CallSub. CallSub doesn't do anything if the target sub (in the activity or service) doesn't exist.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Isnt CallSubDelayed supposed to start the target activity/service? This is why I use it to raise events in different activities.
I also have to use CallSubDelayed when raising events on UI thread, as some events (like touch,onResult etc) cause issues with MsgBox etc.
I always thought it is much safer than CallSub.
 
Upvote 0
Top