How to detect Back Button?

Discussion in 'Android Questions' started by ahwong, Jan 21, 2013.

  1. ahwong

    ahwong Member Licensed 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
     
  2. Omar

    Omar Member Licensed User


    Sub Activity_KeyPress (KeyCode As Int) As Boolean

    If KeyCode = KeyCodes.KEYCODE_BACK Then

    'Perform your functions here

    End If

    End Sub
     
  3. giannimaione

    giannimaione Well-Known Member Licensed 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
     
    mokrokuce likes this.
  4. Pitu

    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.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Please post your Activity_Press code.
     
  6. ac9ts

    ac9ts Active Member Licensed 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.

    Code:
    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
     
  7. Pitu

    Pitu New Member

    Hi Erel... reviewing my code I found that I have used the "Return False" twice :( (...already fixed and now seems it is working fine...)

    Code:
    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
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    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).
     
  9. Pitu

    Pitu New Member

    Thanks! ...clear, short and simple explanation ;)
     
  10. Harish Kumar Arya

    Harish Kumar Arya Member Licensed 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?
     
  11. ac9ts

    ac9ts Active Member Licensed 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:

    Code:
    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: Apr 17, 2015
  12. Sgardy

    Sgardy Member Licensed User

    Just to avoid confusion any additional code must be inserted before RETURN.
     
  13. xor83

    xor83 Member Licensed User

    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?
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    This is an old thread. Please start a new thread and explain what you are trying to do.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice