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
ExitApplication 'App is exiting
Else
Return True
End If
End If
End Sub
How can I take control of the "Back Button" in order to decide to kill or stay in the app?
I saw the implementation of the "Sub Activity_KeyPress (KeyCode As Int) As Boolean" sub (previous post), but whatever I decide inside this sub, then it always goes to the "Activity_Pause" sub and the activity gets destroyed!
I saw many apps showing a modal asking you if you really want to quit or not after pressing the "Back Button", and I would like to implement the same thing in my app: I the user chosse NO, the app should remain foreground, otherwise --> ExitApplication!
The way I do it. It may not be the best but it does the job. This one will exit if you press back twice within a few seconds, not the "Are you sure" scheme but you can always modify.
B4X:
Sub Globals
Dim closeCounter As Int ' For double click exit routine
Dim closeTimer As Long
end sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean
Select KeyCode
Case KeyCodes.KEYCODE_BACK
closeCounter = closeCounter + 1 ' Handle the user exit request
' First time so start the counter
If closeCounter = 1 Then
closeTimer = DateTime.Now
ToastMessageShow("Press Back again to exit", False)
End If
' Second time but not within 3 seconds, reset to "First time"
If closeCounter = 2 AND DateTime.Now > closeTimer + 3000 Then
closeCounter = 1
closeTimer = DateTime.Now
ToastMessageShow("Press Back again to exit", False)
End If
' Two tries within 3 seconds, we're done
If closeCounter = 2 Then
closeCounter = 0
Activity.Finish
End If
Return True
Case Else
Return False
End Select
End Sub
Hi Erel... reviewing my code I found that I have used the "Return False" twice (...already fixed and now seems it is working fine...)
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
If Msgbox2("Are you sure to exit?", "", "Yes", "", "No", Null) = DialogResponse.POSITIVE Then
Return False
ExitApplication '...or whatever other previous killing actions.
Else
Return True
End If
End If
End Sub
What I don't understand is what's the meaning of returning True/False within "Sub Activity_KeyPress"and how does this affect "Activity_Pause"... since I can't put a breakpoint within "Activity_Pause" I am not sure wich one of these two subs is fired first... I guess "Activity_KeyPress".
By returning True from this sub you are telling the OS that you have already handled the back key event and that the OS doesn't need to do anything with it. If the back key event is not handle the default action is applied (which is to call Activity.Finish).
But the IDE underlines sub name with error "not all code paths return a value (Warning #2)", and "ExitApplication" with "Unreachable code detected (Warning #1).
What does this mean?
If you are referring to Pitu's example, I'm guessing it is because the only key press that will return a boolean value is KeyCodes.KEYCODE_BACK
Add a default return value for the other presses:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
If Msgbox2("Are you sure to exit?", "", "Yes", "", "No", Null) = DialogResponse.POSITIVE Then
Return False
ExitApplication '...or whatever other previous killing actions.
Else
Return True
End If
Else
Return False ' Handle the other presses in the OS
End If
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean If KeyCode = KeyCodes.KEYCODE_BACK Then IfMsgbox2("Are you sure to exit?", "", "Yes", "", "No", Null) = DialogResponse.POSITIVE Then ReturnFalse ExitApplication'THIS CODE WILL BE NEVER EXECUTED JUST RETURN FALSE AND THE ACTIVITY WILL STAY ALIVE Else ReturnTrue EndIf Else ReturnFalse' Handle the other presses in the OS EndIf
End Sub
closeCounter = closeCounter + 1' Handle the user exit request ' First time so start the counter If closeCounter = 1Then
closeTimer = DateTime.Now ToastMessageShow("Press Back again to exit", False) EndIf ' Second time but not within 3 seconds, reset to "First time" If closeCounter = 2ANDDateTime.Now > closeTimer + 3000Then closeCounter = 1 closeTimer = DateTime.Now ToastMessageShow("Press Back again to exit", False) EndIf ' Two tries within 3 seconds, we're done If closeCounter = 2Then closeCounter = 0 Activity.Finish EndIf ReturnTrue CaseElse ReturnFalse EndSelect