Back button and Activity finishing

jnbarban

Member
Licensed User
Longtime User
Hi, i want to display a msgbox when i press the back button like :

"Do you realy want exit this application ?"

And if the user say "No", the activity still Active.

How to do this ? When i try :

B4X:
Sub Activity_Pause (UserClosed As Boolean)
   Dim iResultat As Int
   iResultat = Msgbox2("Voulez-vous vraiment quitter l'Etude Conso ?","Etude conso","Oui","","Non",Null)
   If iResultat = DialogResponse.NEGATIVE Then
      UserClosed = False
   End If
End Sub

the msgbox not appear and the Activity finished

thanks...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You cannot cancel the pausing process from Activity_Pause.
Instead you should catch the Back key:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If Msgbox2("Do you want to close?", "", "Yes", "Cancel", "No", Null) = DialogResponse.POSITIVE Then
            Return False
        Else
            Return True
        End If
    End If
End Sub
 
Upvote 0

jnbarban

Member
Licensed User
Longtime User
hello,

when the ProgressDialog is visible, the Activity_KeyPress does'nt work.
How can I intercept the back button in this case ?
Maybe with a special event ?
 
Upvote 0

brainfart

Member
Licensed User
Longtime User
Can one intercept the Home button?
I am building a security device and would like to ask user for PIN before they get out of the app. Does any one know any trick to intercept activity pause/end by any means?
I used the "Sub Activity_KeyPress.... above with success intercepting the Back button but can't seem to catch the Home or Power buttons. Higher power overriding me?

Code:

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
    Dim KC As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then KC = True
    If KeyCode = KeyCodes.KEYCODE_HOME Then    KC = True
    If KeyCode = KeyCodes.KEYCODE_POWER Then KC = True
    If KC = True Then    'if KC was true then one of the buttons that would close the app was pressed. Now we try to stop it from happening
            If Msgbox2("Do you want to close?", "Exit?", "Yes", "", "No", Null) = DialogResponse.POSITIVE Then
            Return False
        Else
            Return True
        End If
    End If
End Sub


Thank you.
 
Last edited:
Upvote 0
Top