How to detect Back Button?

ahwong

Member
Licensed User
Longtime User
Many android phones including my Samsung Note have a Back Button.

What is the method used to detect this button? I want a sub routine to be called when user presses that button...

Thanks
 

Omar

Member
Licensed User
Longtime User
Many android phones including my Samsung Note have a Back Button.

What is the method used to detect this button? I want a sub routine to be called when user presses that button...

Thanks


Sub Activity_KeyPress (KeyCode As Int) As Boolean

If KeyCode = KeyCodes.KEYCODE_BACK Then

'Perform your functions here

End If

End Sub
 
Upvote 0

giannimaione

Well-Known Member
Licensed User
Longtime User
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
 
Upvote 0

Pitu

New Member
Hi everyone... I'm new and this is my first post.

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!

Thanks in advance.
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
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
 
Upvote 0

Pitu

New Member
Please post your Activity_Press code.

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".

Thanks in advance
 
Upvote 0

Harish Kumar Arya

Member
Licensed User
Longtime User
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?
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
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
 
Last edited:
Upvote 0

Sgardy

Member
Licensed User
Longtime User
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 'THIS CODE WILL BE NEVER EXECUTED JUST RETURN FALSE AND THE ACTIVITY WILL STAY ALIVE
Else
Return True
End If
Else
Return False ' Handle the other presses in the OS
End If
End Sub

Just to avoid confusion any additional code must be inserted before RETURN.
 
Upvote 0

xor83

Member
Licensed User
Longtime User
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

I used the above code but my activity reloads once and i have to click back again to close the activity, why this is happening?
 
Upvote 0
Top