Disable Home button in an activity

// Expert user has replied.
P Pietro Francesco Maggi 3 years 7 months ago
687 1 0

Given that in Android is not possible to intercept the Home button in the onKeyDown event.
For example if try this code in an Android app, you never get the "Home button pressed!" log:
 
  @Override public boolean onKeyDown(int keyCode, KeyEvent event) {     if(keyCode == KeyEvent.KEYCODE_BACK)     {         Log.d("Test", "Back button pressed!");     }     else if(keyCode == KeyEvent.KEYCODE_HOME)     {         Log.d("Test", "Home button pressed!");     }     return super.onKeyDown(keyCode, event); }   
You have two different possibilities:

Use a intent-filter in the application manifest.xml (but this is not restricted to an application and Android show a list of the available home screens).
Use a TYPE_KEYGUARD Layout Parameter for the Windows Manager.

 
Intent-Filter

Starting from an android application, you can add to the intent-filter section of the AndroidManifest.xml file some code to have it as:
 
                                                                                                                                                                            
The problem with this solution is that you get a selection box  (till you select a default) that let you choose which intent you want to use.
 
TYPE_KEYGUARD
 
Add this code to your intent:
 
@override  public void onAttachedToWindow() {          this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);             super.onAttachedToWindow();   }    
Then you have to use for your activity the theme:
 
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"           
This disable the Home Button and hide the notification bar.
 
Pietro

Please Register or Login to post a reply

1 Replies

K Kultida Chatarupachewin

Hi Pietro,

Does this method work on Jelly Bean? My customer wants to disable Home button on JB, because it just Exit their application right away when pressing Home button. Unlike GB, it will notify user first for confirmation whether he want to exit the app or not.

thank you,
Kultida

CONTACT
Can’t find what you’re looking for?