How to make " asking permission before exiting the App?"
When pressed Exit in Android device, it should ask permission, before exiting.
Please tell me the way..
Here's how I do it. There may be better ways but this works for me.
Basically, you trap the back button press, grab the time, and, if back is pressed again before a set time period, you exit.
B4X:
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim closeCounter As Int ' For double click exit routine
Dim closeTimer As Long
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean
Select KeyCode
Case KeyCodes.KEYCODE_BACK
' Handle the user exit request from the main screen
closeCounter = closeCounter + 1
' 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
Thank you very much, ac9ts.
instead of ToastMessageShow("Press Back again to exit"), how to present a form with: "Do you want to Exit?"-- "OK" and "Cancel".
ie, a form with " Do you want to Exit? " "OK" and "Cancel".
it works with "KeyCodes.KEYCODE_BACK", but doesn't work with "KeyCodes.KEYCODE_HOME", the home button.
Why this doesn't work with Home Key? If Home key pressed, the app goes to exit mode.
Please help me..
you can not capture the click on the home button. You app goes to the background (activity paused). You can not prevent this. This is how Android works.
Thank you very much, ac9ts.
instead of ToastMessageShow("Press Back again to exit"), how to present a form with: "Do you want to Exit?"-- "OK" and "Cancel".
ie, a form with " Do you want to Exit? " "OK" and "Cancel".
You can replace the toast messages in my example with whatever you want. The routine displayed just showed a way to capture the back button presses and give a simple example of how to exit. You can see the basic flow and change the prompts and/or action as you need.
Sub Activity_KeyPress(KeyCode As Int)As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then ' --> Button "BACK" - ExitApplication for a back press.
Dim result As Int
result = Msgbox2("Do you wish to Exit application?", "", "Exit", "Cancel", "", Null)
If result = DialogResponse.CANCEL Then
Return True
Else
Activity.Finish
End If
End If
End Sub
The best way to show a dialog when the user clicks on the back key is:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
If KeyCode = KeyCodes.KEYCODE_BACK Then
HandleBackKey
Return True
End If
Return False
End Sub
Private Sub HandleBackKey
Msgbox2Async(Message, "Close?", "Yes", "No", "", Null, False)
Wait For MsgBox_Result (Result As Int)
If Result = DialogResponse.POSITIVE Then
Activity.Finish
End If
End Sub