detecting home and back keys?

rfresh

Well-Known Member
Licensed User
Longtime User
How can I detect when the user presses the home and back keys?

I thought Activity_Pause() would trap it but apparently not.

Thanks...
 

NJDude

Expert
Licensed User
Longtime User
The HOME key CANNOT be trapped, only the BACK key.
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean

    If KeyCode = KeyCodes.KEYCODE_BACK Then

       ... Some Code

    End If

End Sub

More info about KeyCodes HERE
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Ok thanks.

If you press the Home key, the Activity_Pause() fires yes? (But you can't tell why it fired).
 
Upvote 0

Arun

Member
Licensed User
Longtime User
Activity_Pause not fired when pressing Home

The Activity_Pause event is not getting fired when Home key is pressed.

The Home key just closes the app without any warning.

(This is part of a larger app where it wasn't working, therefore I just put it in a new project)

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   Dim result As Int
   result = Msgbox2 ("QUIT?",KeyCode & " PRESSED","YES","","NO",Null)
   If result=DialogResponse.POSITIVE Then
      Msgbox ("Entered YES","")
      Return False
   Else
      Msgbox ("Entered NO","")
      Return True
   End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
'   If UserClosed = True Then
      Msgbox("Activity closed in PAUSE EVENT","")
      ExitApplication
'   End If

End Sub

The attached project contains the above code in ready to run form.

Thanks in advance!
 

Attachments

  • HomeKeyPress.zip
    5.9 KB · Views: 524
Upvote 0

Arun

Member
Licensed User
Longtime User
Thanks - Problem solved

:signOops: Sorry! My bad! :eek:

Actually, I need my app to be persistent and found workarounds for almost all exit options except Home keypress.

I thought, along with android, b4a also ganged up against me in detecting Home keypress. :BangHead:

In all the trial and error, I missed out on the obvious.

Thanks a lot for the prompt reply.:D
 
Upvote 0
Top