Android Question Call startActivityForResult from within a class module?

ThePlankton

Member
Licensed User
I'm attempting to use the camera with intents from a class module. I've taken the code from the sample and modified the GetBA function. It works most of the time but maybe 10-20% of the time I never get the result callback. The times that it doesnt I get this in the log :

onActivityResult: IOnActivityResult was released

Not sure what I'm doing wrong

** I believe I found the issue. I was creating the class instance in a button click event, needed to be in Globals

B4X:
Sub Class_Globals
  
    Private ion As Object
    Private const tempImageFile As String = "tempimage.jpg"
  
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize()

End Sub

Sub TakePicture
    Dim i As Intent
    i.Initialize("android.media.action.IMAGE_CAPTURE", "")
    File.Delete(Starter.provider.SharedFolder, tempImageFile)
    Dim u As Object = Starter.provider.GetFileUri(tempImageFile)
    i.PutExtra("output", u) 'the image will be saved to this path
    Try
        StartActivityForResult(i)
    Catch
        ToastMessageShow("Camera is not available.", True)
        Log(LastException)
    End Try
End Sub

'result arrives here
Sub ion_Event (MethodName As String, Args() As Object) As Object
    ToastMessageShow("Args(0)=" & Args(0), True)
    If Args(0) = -1 Then
        Try
            Dim in As Intent = Args(1)
            If File.Exists(Starter.provider.SharedFolder, tempImageFile) Then
                CallSub(Starter, "UploadLogs")
            Else If in.HasExtra("data") Then 'try to get thumbnail instead
                Dim jo As JavaObject = in
                'lastPicture = jo.RunMethodJO("getExtras", Null).RunMethod("get", Array("data"))
            End If
        Catch
            Log(LastException)
        End Try
    End If
    'If lastPicture.IsInitialized Then ImageView1.Bitmap = lastPicture
    Return Null
End Sub


Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject = Me
    Return jo.RunMethod("getBA", Null)
End Sub
 
Last edited:
Top