Wish Allow Wait For in Code Modules

JohnC

Expert
Licensed User
Longtime User
Many, many, many times when I create a cool new general purpose routine that I would like to share in multiple projects, I am often unable to place it in a code module if the routine has a "Wait For" in it because a code module is a static module that can't handle events.

For example, the below commonly used routine would be very handy to have in a code module so that I did NOT have to copy and paste it in each activity or service module that used it:

B4X:
Sub CheckLocationPermission(Reason as String) As ResumableSub
    If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
        MsgboxAsync(Reason,"Location Permission")  'display meaningful reason to user before displaying cryptic android dialog
        Wait For MsgBox_Result (Result As Int)

        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
        Wait For Activity_PermissionResult (Permission As String, AskResult As Boolean)
        If AskResult = False Then
            Return False
        Else
            Return True
        End If
    Else
        Return True
    End If
End Sub

But, what if I could pass the calling activity to the routine so that the routine could then use it to handle the events?

For example, it would be cool if I could call the above routine from an activity and pass the activity as a parameter:

B4X:
Wait for CodeModule.CheckLocationPermission("So this app can record your hiking trail, you need to grant permission on the next screen", Me) (Result as Boolean)

And then the routine (in the code module) would be designed to use the passed activity to handle the events (in the code module):

B4X:
Sub CheckLocationPermission(Reason as String, EventActivity as Activity) As ResumableSub
    If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
        MsgboxAsync(Reason,"Location Permission")  'display meaningful reason to user before displaying cryptic android dialog
        Wait For MsgBox_Result (Result As Int) Using EventActivity

        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
        Wait For Activity_PermissionResult (Permission As String, AskResult As Boolean) Using EventActivity
        If AskResult = False Then
            Return False
        Else
            Return True
        End If
    Else
        Return True
    End If
End Sub

Being able to include routines (that can handle certain events) in code modules would drastically improve code reusability. (The reason why a Class module would not be very useful is that my code modules have many, many general purpose routines in it and I am trying to avoid having a bunch of separate include files)

Think of all the time it will save not having to duplicate or copy and paste a commonly used routine into an activity or service just because the routine uses events so it can't be placed in a shared code module.

Thoughts?
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
In the activity, have
B4X:
Sub Activity_PermissionResult (Permission As String, AskResult As Boolean)
        CallSubDelayed3(nameofcodemodule, “Activity_PermissionResult”, Permission, AsResult)
End Sub
You can do this with the msg box event too (if needed). Btw, even in a class module, the Activity_PermissionResult event would fire in the activity, not the class. I use the above work around to call the event then in the class. I’ve not tried the above solution with a code module. Btw, I’m pretty sure that code modules can handle events. This could also be platform specific (works in B4J). Note: some events are only raised at the Activity level (Activity_PermissionResult being one of them), no matter if called from a class or (in your case) code module
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't use code modules at all. Use a class instance.

You can create a single class with all the features you need and initialize it in the starter service.
Code modules in B4A will never support events due to the way context works in Android. Code modules do not have a context of their own.
Note that it is not the case in B4J and B4i.
 
Top