Intercepting User Close

Dave Fitzpatrick

Member
Licensed User
Longtime User
Is there any way to intercept the user close action and fire a dialog that asks the user if they really want to do that? I notice that there are some apps out there that seem to do this, i.e. Astro, for example, which raises a ToastMessage telling you to "Press Back again to Exit"

Dave
 

mc73

Well-Known Member
Licensed User
Longtime User
Sure. Trap the 'back' key in the keypress sub and show a toastMessage. Increase a counter to 1. Next time you trap the 'back' key, check for this counter. If 1, then finish the activity.
 
Upvote 0

Dave Fitzpatrick

Member
Licensed User
Longtime User
Well, I include this sub:

Sub Activity_KeyPress(KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
closeCounter = closeCounter + 1
If closeCounter = 2 Then
closeCounter = 0
Activity.Finish
Else
ToastMessageShow("Press Back again to Exit", True)
End If
End If
End Sub

When I hit the Back button, the ToastMessage displays, but the Activity closes anyway. How do I stop the activity from closing?
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
This should work:
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean

If KeyCode = KeyCodes.KEYCODE_BACK Then

closeCounter = closeCounter + 1

If closeCounter = 2 Then
closeCounter = 0
Activity.Finish

Else

ToastMessageShow("Press Back again to Exit", True)
Return True '<--- Add this

End If

End If

End Sub

Also, when posting code, use the [noparse]
B4X:
...
[/noparse] tags.
 
Upvote 0
Top