Android Question KeyPress crash

Discussion in 'Android Questions' started by nibbo, Jun 25, 2019.

  1. nibbo

    nibbo Active Member Licensed 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.

    Code:
    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...
     
  2. rosippc64a

    rosippc64a Active Member Licensed User

    I encountered problem like this, and the solution was that I organised the code as is:
    Code:
    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
     
    nibbo likes this.
  3. nibbo

    nibbo Active Member Licensed 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.

    Code:
    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
     
  4. MarkusR

    MarkusR Well-Known Member Licensed User

    are you sure this "BD.Msgbox" is not blocking the main thread?
     
  5. nibbo

    nibbo Active Member Licensed User

    Thanks for the suggestion MarkusR

    I commented out the CallSubDelayed3 and ran it like this:

    Code:
    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
     
  6. MarkusR

    MarkusR Well-Known Member Licensed User

    mysterious
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Don't use modal dialogs.

    Use the async version. If you need custom dialogs then use B4XDialogs or other libraries that provide async versions.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice