Translate

Sunday, February 16, 2020

Android - how disable the Home button


Here a way to disable the Home button (but also the Back and Menu).

CAUTION !!
Possible undesired side effects !  Use at your risk !

It is assumed you know how to work on Android and I'm talking about !

First of all we need to add some permission and defines in the Manifest file :

<uses-permission android:name="android.permission.REORDER_TASKS" />

then in the main activity or start activity, in the intent filter section :

<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />

At this point in the main or base activity, assuming you have part of the code in common for all the activities add :

@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        // Pressed HOME !!! Ignore it !        return false;
    }
    return super.onKeyDown(keyCode, event);
}

@Override protected void onPause() {
    super.onPause();

    ActivityManager activityManager = (ActivityManager) getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);

    activityManager.moveTaskToFront(getTaskId(), 0);
}

On some Android version the first time running the app, the pressing of the Home button could trigger a pop up asking to reassign the Home button to the app.
If select Always, will force the Home button to be reassigned to the app itself.
It means that pressing the Home button is the equivalent to re-launch the app, so a possible side effects can happens.
i.e. the application need to be designed to take care of this different pattern

Is possible of course to intercept other buttons as well, like :

  • Home button  (KeyEvent.KEYCODE_HOME ) 
  • Back button ( KeyEvent.KEYCODE_BACK )
  • Menu/Mode ( KeyEvent.KEYCODE_MODE )


No comments:

Post a Comment