Android Question Event not being called in Main Activity from Class Module

Falcon

Member
Licensed User
Hi Gurus,

OK so I have a Helper Class Module I made called 'JRHelper' and in there I have a sub which will show the Device's Image Gallery and then wait for the user to pick an image.
I then want to call a Event sub in my Main Activity once the image has been picked, but the problem is that the Event is only called if I step through the code with a Breakpoint, if I just
run it (either in Debug or Release mode) it will not call the Event.

The code in my Main Activity:

B4X:
Sub Globals
     Private JRHelper_ As JRHelper            'My Helper Class Module.
End Sub


Sub Activity_Create(FirstTime As Boolean)
   JRHelper_.Initialize(Me,"JRHELPER")    'Initialise my Helper Class Module.
End Sub


Sub Button1_Click  
    JRHelper_.ShowImageGallery()  
End Sub


'This is the Event Sub I am trying to call from my Class Module.
Private Sub JRHELPER_ImageGalleryFileSelected(strRealFilePathName As String, strVirtualFilePathName As String)

    'Get the Image's File Name and it's Path separately.
    Dim strRealFileName As String = JRHelper_.GetFileName(strRealFilePathName)
    Dim strRealPath As String = JRHelper_.GetPath(strRealFilePathName)

    'Make the image's Max Height or Width 300 pixels to reduce size & loading times in the Database.
   Dim ReducedSizeBitmap As Bitmap = JRHelper_.ResizeImage(strRealPath,strRealFileName,300)

End Sub


In my Class Module I have the following code:

B4X:
'Declare my Event which is in the Main Activity.
#Event: ImageGalleryFileSelected(strRealFilePathName as string, strVirtualFilePathName as string)

Sub Class_Globals
    Private Caller_ As Object                  'The Main Activity.
    Private strEventName_ As String      'The event prefix.
End Sub


Public Sub Initialize(Caller As Object,strEventName As String)
    Caller_ = Caller                                 'Get my Main Activity 
    strEventName_ = strEventName     'Get my event prefix.
End Sub


Public Sub ShowImageGallery()
     
    'Prompt the user with a message box to grant permission for access to external storage.
    RequestPermissionToReadExtStorage
            
    'Show the Device's Image Gallery.
    Dim chooser As ContentChooser
    If chooser.IsInitialized = False Then
        chooser.Initialize("chooser")
    End If
   
    chooser.show("image/*", "Choose Image")
    
    Wait For(chooser) chooser_Result (Success As Boolean, Dir As String, FileName As String)
         
    If Success Then
         
        
        Dim strRealFilePathName As String = GetRealFilePathNameFromContentChooser(FileName)
        Dim strVirtualFilePathName As String = Dir & ";" & FileName

        'Call my Event in the Main Activity   <----- This only works when I step it with a Break Point.
        CallSubDelayed3(Caller_, strEventName_ & "_ImageGalleryFileSelected",strRealFilePathName,strVirtualFilePathName)
              
    Else
        'No image was selected.
    End If
     
End Sub


Public Sub RequestPermissionToReadExtStorage

    RuntimePermission.CheckAndRequest(RuntimePermission.PERMISSION_READ_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    Sleep(50)

End Sub
 

LucaMs

Expert
Licensed User
Longtime User
One thing that I see is that you are not using 'CallSub' at all, so I guess that has solved the issue
The main problem was trying to request permissions from inside the class.

Other problems were that when you have Resumable routines (routines containing "Sleep" or "Wait For"), they immediately return the flow to the calling routine whereas in some cases you want the called routine to have completed. That's why I wrote that link in #13
 
Upvote 0
Top