如何处理操作栏中的“后退”按钮?

时间:2015-09-03 20:56:23

标签: android android-studio

我试过这个但没有任何反应。

@Override
public void onBackPressed() {
    super.onBackPressed();
    Log.d("TEST", "Pressed the Back button.");
}

enter image description here

2 个答案:

答案 0 :(得分:1)

操作栏上的后退按钮是ID为android.R.id.home的菜单项。以下是处理点击项目的方法:

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
        case android.R.id.home:
            // do something useful
            return true;
    }

    return super.onOptionsItemSelected(item);
}

答案 1 :(得分:0)

OnBackPressed不是指那个按钮,而是指屏幕底部的按钮(家里的按钮)

您正在寻找的是在主页按钮上自定义行为

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
       //TODO log here
        return true;
}
return super.onOptionsItemSelected(item);

}`

相关问题