我尝试过以下方法来检测活动中的反压:
private static final String TAG = "PRESSED";
@Override
public void onBackPressed() {
Log.i(TAG,"BACK");
super.onBackPressed();
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.i(TAG, "Back pressed");
}
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.i(TAG, "Back pressed 1");
}
return super.onKeyDown(keyCode, event);
}
更新
按下按钮上的onKeyDown,onKeyUp和onBackPressed WORK" On screen buttons"在屏幕的底部,这是:
为什么按下标题栏中的“向上”按钮时它们不起作用?
答案 0 :(得分:2)
也许这个? 它使ActionBar后退按钮像后退按钮一样工作。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:0)
这是您主要活动中的代码
@Override
public boolean onNavigateUp() {
// TODO Auto-generated method stub
getFragmentManager().popBackStack();
return true;
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// for closing activity
boolean canback = getFragmentManager().getBackStackEntryCount()==1;
if (canback) {
finish();
}
else {
super.onBackPressed();
}
}