我正在为Android v-21使用appcompat
活动。我想启用主页按钮,该按钮已在代码中设置为true
。我也重写了onOptionsItemSelected
,但是它仍然无法正常工作。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apply_card);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//Action bar
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}
最佳答案
在Appcompat活动中添加操作栏主页启用的简单方法
getSupportActionBar().show();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
您还可以在活动中添加这两个公共功能-
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
}