Android Question KeyPress crash

nibbo

Active Member
Licensed User
Longtime User
Hi All

I have several handheld devices running Android 8.1 that have some extra rubber keys.
There is a home key, F1-F2 keys & a Menu key.

I don't think the Home key can be intercepted (if anyone knows otherwise I would love to be able to prevent its use) but the F1, F2 & Menu buttons do fire the KeyPress event.
They are KeyCodes 285, 132 & 54 respectively.

If I have an Activity_KeyPress sub then it fires but crashes on the Return statement, if I comment out my Activity_KeyPress then the app runs OK. Unfortunately I do want to handle the back key so I need Activity_KeyPress sub.

Below is a copy of the crash report (there is more if it would help):


Below is my Activity_KepPress code which I use in several apps without a problem, only when using these devices.

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
   
    '    handle the back key
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        '    get confirmation that the user wished to exit
        Dim res As Int
        Dim BD As BetterDialogs
        res = BD.Msgbox("Please confirm","Are you sure you wish to exit the application.","Yes","","No",Null)
        If res = -2 Then
            '    keep app open
            Return True
        Else
            '     close the app
            Main.iResume = 1
            Activity.Finish
        End If
    End If
   
    Return False
   
End Sub

Thanks for any ideas...
 

rosippc64a

Active Member
Licensed User
Longtime User
I encountered problem like this, and the solution was that I organised the code as is:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    return CallSubDelayed2(Me,"thprocess",KeyCode)
end sub

Sub thprocess( keycode As Int) as boolean
    '    handle the back key
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        '    get confirmation that the user wished to exit
        Dim res As Int
        Dim BD As BetterDialogs
        res = BD.Msgbox("Please confirm","Are you sure you wish to exit the application.","Yes","","No",Null)
        If res = -2 Then
            '    keep app open
            Return True
        Else
            '     close the app
            Main.iResume = 1
            Activity.Finish
        End If
    End If
  
    Return False
  
End Sub
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
Interesting but... I don't think you can return a value from CallSubDelayed2; I got an error 'Cannot assign void value'.
CallSub2(…) crashes in the same way as.

What does seem to work is CallSubDelayed3 and using a Boolean in Argument2 to contain the result, thanks for pointing me in the right direction.

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Dim bResult As Boolean = False
    CallSubDelayed3(Me, "ProcessKeyPress", KeyCode, bResult)
    Return bResult
End Sub

Sub ProcessKeyPress(keycode As Int, Result As Boolean)
    
    '    handle the back key
    If keycode = KeyCodes.KEYCODE_BACK Then
        '    get confirmation that the user wished to exit
        Dim Res As Int
        Dim BD As BetterDialogs
        Res = BD.Msgbox("Please confirm","Are you sure you wish to exit the application.","Yes","","No",Null)
        If Res = -2 Then
            '    keep app open
            Result = True
            Return
        Else
            '     close the app
            Main.iResume = 1
            Activity.Finish
        End If
    End If
    
    Result = False
    
End Sub
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
are you sure this "BD.Msgbox" is not blocking the main thread?
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
are you sure this "BD.Msgbox" is not blocking the main thread?
Thanks for the suggestion MarkusR

I commented out the CallSubDelayed3 and ran it like this:

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Dim bResult As Boolean = False
    'CallSubDelayed3(Me, "ProcessKeyPress", KeyCode, bResult)
    Return bResult
End Sub

Still crashes.

It looks like this is only in Debug mode so less concerned about it now.

Ta
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
mysterious
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…