Android Question Registering a callback from a code module

rzv25

Member
Licensed User
Longtime User
Hello all,

It is very possible for my problem to have a very simple solution, but I did not find anything searching the forum, so I apologize in advance if this is the case.

The functionality of my application is split in code modules. So for example I have a GPS code module that offers a asynchronous service for getting the current location. The service is used by other code modules, like for example the smsParser. I would like to call for example from smsParser module the Gps_Start() function in the Gps module sending as parameter the name of a callback residing in smsParser module. This callback should be called when Gps process of determining current location is finished.

I thought I could just pass the name of the callback to the Gps_Start() sub as string and then use CallSub() from Gps module when location is determined, but the comments of the CallSub function states: 'Note that it is not possible to call subs of code modules'.

I appreciate any ideas that you have.

Thank you.
 

rzv25

Member
Licensed User
Longtime User
Me again.
I have created a class that has one event but now I have another dilemma. The 'Initialize' method of the class has 2 parameters, like in the example provided in UserGuide:
B4X:
Public Sub Initialize (vCallback As Object, vEventName As String)
    EventName = vEventName
    CallBack = vCallback
End Sub

and the event from the class is called like this:
B4X:
If SubExists(Callback, EventName & "_ValuesChanged") Then
CallSub(Callback, EventName & "_ValuesChanged")
End If

I declare an instance of the class in a code module and I need to pass it as first parameter an object. What should I pass, as Me keyword does not work in code modules ?

I saw some libraries (like GPS for example) have only one parameter for Initialize method, and that is the event name. So I can declare an object of type GPS in my code module and have the event for location changed implemented in the code module. Is this possible with a class ? If yes, how should I make the call to the event from the class' code ?

Thank you
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that the SubExists check is not really needed. Nothing will happen if the sub doesn't exist.

I declare an instance of the class in a code module and I need to pass it as first parameter an object. What should I pass, as Me keyword does not work in code modules ?
Code modules cannot handle events. Move it to the starter service (and use Me).
 
Upvote 0

rzv25

Member
Licensed User
Longtime User
Thank you for the reply, Erel. I would like to organize my code based on functionality, in code modules. I don't want to move everything in starter service.


What about the other question I had ?

I saw some libraries (like GPS for example) have only one parameter for Initialize method, and that is the event name. So I can declare an object of type GPS in my code module and have the event for location changed implemented in the code module. Is this possible with a class ? If yes, how should I make the call to the event from the class' code ?

Thank you
 
Upvote 0
Top