7

I have override the OnBackPressed function inside my activity, but it's not being called. On other activities it's working fine. Here is my method:

@Override
public void onBackPressed() {
    Log.e("back",""+1); 
    UserPage.getstate().finish();
    Intent i=new Intent(CreateGroup.this,UserPage.class);
    i.putExtra("title11","dd");
    startActivity(i);
    finish();
}

This method is not being called, and the default OnBackPresssed is called every time i press the back button.

4

4 回答 4

8

Try this code

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Log.e("back",""+1); 
        UserPage.getstate().finish();
        Intent i=new Intent(CreateGroup.this,UserPage.class);
        i.putExtra("title11","dd");
        startActivity(i);
        finish();
    }
    return true;
}
于 2013-05-20T09:25:33.697 回答
8

Had a similar issue. The problem for me was in my override of onKeyDown() hiding the KEYCODE_BACK keydown events.

于 2014-07-09T21:27:23.913 回答
4

Have you tried this? ,

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


        return true;
    }

    return super.onKeyDown(keyCode, event);
}
于 2013-05-20T09:25:03.600 回答
2
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Log.e("back",""+1); 
        UserPage.getstate().finish();
        Intent i=new Intent(CreateGroup.this,UserPage.class);
        i.putExtra("title11","dd");
        startActivity(i);
        finish();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
于 2013-05-20T09:45:44.153 回答