Android Question intercept the CameBack and Home Button.

Semen Matusovskiy

Well-Known Member
Licensed User
WIth Back button all is simple - search by KeyCodes.KEYCODE_BACK and you will find example.
Home button is busy by system. It's possible to declare an app as "launcher", but better not to do this. User expects default system actions.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
how to intercept the CameBack
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
     If KeyCode = KeyCodes.KEYCODE_BACK Then
          If Msgbox2("Exit?","Exit to App","Yes","","No",Null)<> DialogResponse.POSITIVE Then Return True
     End If
     Return False
End Sub

and Home Button.
It is not possible. When you click the Home button the App is not killed (as in the case of the Back button) but is put in the background.

You could put a check and delay the exit or put the App back in the foreground (Maybe you could start a service that reopens the App in the foreground, but I've never tried is just a hypothesis)

B4X:
Sub Activity_Pause (UserClosed As Boolean)
     If UserClosed  Then
         Sleep(3000)
        'It will give the time to the unfinished services to complete the operations
        '(Astrem, for example, transmissions on bluetooth printers and ecc.)
    End If
End Sub
 
Last edited:
  • Like
Reactions: eps
Upvote 0
Top