Android onKeyDown() not execute on pressing back button

Hardik Joshi picture Hardik Joshi · Aug 6, 2012 · Viewed 23.8k times · Source

Hi after searching on google i come here to ask about help. I use following method to go back when user press back button of device.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {


        Log.i("===BACK BUTTON PRESSED===", "BACK BUTTON");

        return true;
    } else {
        Log.i("===ELSE BACK BUTTON PRESSED===", "ELSE BACK BUTTON");

        return super.onKeyDown(keyCode, event);
    }
}

But logcat does not display any log message.

W/KeyCharacterMap(517): No keyboard for id 0  

W/KeyCharacterMap(517): Using default keymap: /system/usr/keychars/qwerty.kcm.bin Please help me to find this.

EDITED QUESTION :

This is my entire class.

public class Dreams_Reminder_detail_screen extends Activity {   

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        GroupDreams.group.back();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dreams_reminder_details_screen);        
}

}

Answer

rajpara picture rajpara · Aug 6, 2012

You can override onBackPressed() function and implement your logic there.

Please refer this android blogpost "Back and other hard keys: three stories" for more understandings.

You can overrided onKeyDown function in GroupDreams perhaps that will work