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):

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x54 in tid 3111 (eryConfirmation), pid 3111 (eryConfirmation)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'Remdun/RD52/RD52:8.1.0/O11019/1543226686:user/release-keys'
Revision: '0'
ABI: 'arm'
pid: 3111, tid: 3111, name: eryConfirmation >>> Company.DeliveryConfirmation <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x54
Cause: null pointer dereference
r0 00000054 r1 00000001 r2 0000000c r3 00000001
r4 c6bcc010 r5 00000000 r6 c6bcc024 r7 e7e4cf51
r8 00000000 r9 e50b393c sl 00000063 fp e50b38f0
ip e7f0ad80 sp ffb23938 lr e7e646d7 pc e6d26204 cpsr 200d0030
backtrace:

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
Top