23

I know that one can overwrite the Back Button functionality in Android, but I was wondering if there was a method or anything I could call that would functionally do the same thing as pressing the hardware button.

Cœur
  • 37,241
  • 25
  • 195
  • 267
JuiCe
  • 4,132
  • 16
  • 68
  • 119

3 Answers3

28

You can send the back button press to the system like this

this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); 

Or you can override the back button press and call

finish();

on your Activity. That basically does the same thing as the generic back button.

Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
Frank Sposaro
  • 8,511
  • 4
  • 43
  • 64
  • Doesn't work in my code...tells me to change it to ACTION_DOWN, and KEYCODE_BACK don't exist, what would I need to import for them to be found? – JuiCe Jul 31 '12 at 19:23
  • 1
    Try using KeyEvent.ACTION_DOWN and KeyEvent.KEYCODE_BACK :) – jimmithy Jul 31 '12 at 19:26
  • Thanks @jimmithy I updated the solution. Hopefully that was the correct answer you were looking for :) – Frank Sposaro Jul 31 '12 at 19:30
  • 5
    This did not work for me. But found this workaround that others might find useful too: dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK)); http://stackoverflow.com/a/14891899/555856 – Scrat May 24 '13 at 12:34
  • This isn't entirely correct. You are literally dispatching a key event when the button is pressed down and then up. Did you try removing just the ACTION_DOWN event and see if it still works? – Frank Sposaro May 24 '13 at 15:34
22

Just call this.onBackPressed(); in Activity.

frogatto
  • 28,539
  • 11
  • 83
  • 129
hoi
  • 2,168
  • 20
  • 22
  • This is a great answer. – Vahid Amiri Aug 24 '16 at 20:11
  • 1
    This one is the best answer, as simply calling finish doesn't trigger onBackPressed like the real back button does. If the activity is doing extra stuff there it'll be missed. – Andrew Dec 23 '16 at 03:51
  • But like if there is any dialog over activity then it will close activity, and real button doesn't do that. – NehaK Feb 23 '18 at 10:09
  • @NehaK In that case, you can override onBackPressed() If the dialog is showing then dismiss it first. – hoi Feb 28 '19 at 08:14
8

If you want just "press" hardware button, create service extended from AccessibilityService:

class ExampleAccessService:AccessibilityService() {
    override fun onInterrupt() {
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
    }
    
    fun doAction(){
        performGlobalAction(GLOBAL_ACTION_RECENTS)
//        performGlobalAction(GLOBAL_ACTION_BACK)
//        performGlobalAction(GLOBAL_ACTION_HOME)
//        performGlobalAction(GLOBAL_ACTION_NOTIFICATIONS)
//        performGlobalAction(GLOBAL_ACTION_POWER_DIALOG)
//        performGlobalAction(GLOBAL_ACTION_QUICK_SETTINGS)
//        performGlobalAction(GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN)
    }
}

Call doAction() where you want action

Add to Manifest:

<application
...
    <service
        android:name=".ExampleAccessService"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:label="Name of servise" // it will be viewed in Settings->Accessibility->Services
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService"/>
        </intent-filter>
        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/accessibility_service_config"/>
    </service>
...
</application>

accessibility_service_config.xml:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:accessibilityFlags="flagDefault"
    android:canRetrieveWindowContent="false"
    android:description="your description"
    android:notificationTimeout="100"
    android:packageNames="your app package, ex: ex: com.example.android"
    android:settingsActivity="your settings activity ex: com.example.android.MainActivity" />

for more info look at https://developer.android.com/guide/topics/ui/accessibility/service

Evgenii Vorobei
  • 1,457
  • 18
  • 20