Google+

Tuesday, July 19, 2011

Disable Back Key (Button on Device) in Android

Here is the code to disable device back key in android 
 
 
Override the onKeyDown activity method. And check for Back Key Event using KeyEvent.KEYCODE_BACK with 
Key Hit Count using event.getRepeatCount()==0

event.getRepeatCount use to get number of time key pressed.
if the both the condition true call any customized method where your business logic will be there.
Here I use onBackPressed(); and return nothing.
 
Note : if you use statement return in onKeyDown Method you will get ERROR
So better idea is to use any method and write just return. 
 
 
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if ( keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

        onBackPressed();
    }

    return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {

    return;
}
 
 
 
 
 
Welcome for All Comments........ :) 

1 comment:

IT Professional said...

its verryyyyyyyyyyy niz dude..we are indians

Google+