6

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

}

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113

6 Answers6

8

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

rajpara
  • 5,203
  • 1
  • 29
  • 46
  • i just put log in keydown function and it get printed, what's your issue now ?? i don't know about "GroupDreams.group.back();" because this will be your custom function. – rajpara Aug 06 '12 at 06:30
  • I work with custometab activity. GroupDreams is my ActivityGroup. so if user press back buuton i go back to activity of GroupDreams. But does not work. – Hardik Joshi Aug 06 '12 at 06:32
  • your activity is getting closed , if this is your issue then just remove moveTaskToBack(true); – rajpara Aug 06 '12 at 06:32
  • I remove moveTaskToBack(true); but does not go back. i am so so stuck about this. – Hardik Joshi Aug 06 '12 at 06:37
  • please refer this blogpost this show multiple activity under single tab with back button functionality http://ericharlow.blogspot.in/2010/09/experience-multiple-android-activities.html – rajpara Aug 06 '12 at 06:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14936/discussion-between-android-coader-and-hardik-joshi) – rajpara Aug 06 '12 at 06:42
3

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);
}
Community
  • 1
  • 1
1

You can trace the Back Button Event this Way :-

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

     //Do the Logics Here 
}
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
1

Try with following code it will help you

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) 
        {
             }
         }
koti
  • 3,681
  • 5
  • 34
  • 58
1
    @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;
}
turbandroid
  • 2,296
  • 22
  • 30
HarrY
  • 139
  • 10
0

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

}
Dhruvil Patel
  • 2,910
  • 2
  • 22
  • 39