Hardik Joshi
Hardik Joshi

Reputation: 9507

Android onKeyDown() not execute on pressing back button

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);        
}

}

Upvotes: 6

Views: 25289

Answers (6)

HarrY
HarrY

Reputation: 139

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
//How to intercept the BACK key in an Activity is also one of the common questions we see //developers ask, so as of 2.0 we have a new little API to make this more simple and easier //to discover and get right:

@Override
public void onBackPressed() {
// do something on back.
return;
}

Upvotes: 1

rajpara
rajpara

Reputation: 5203

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

Upvotes: 8

user1357696
user1357696

Reputation:

Try this link: Override back button to act like home button

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: 3

Dhruvil Patel
Dhruvil Patel

Reputation: 2930

you wrote super method in else{} that was wrong. Edit your code like this

    @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);

}

Upvotes: 0

koti
koti

Reputation: 3701

Try with following code it will help you

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) 
        {
             }
         }

Upvotes: 1

Haresh Chaudhary
Haresh Chaudhary

Reputation: 4400

You can trace the Back Button Event this Way :-

@Override
public void onBackPressed() {
    super.onBackPressed();

     //Do the Logics Here 
}

Upvotes: 1

Related Questions