最近在完善一個issue,其中需要給新創建的頁面添加返回按鈕,代碼非常簡單:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//給左上角添加一個返回的箭頭圖標
getSupportActionBar().setHomeButtonEnabled(true);//設置圖標可以點擊
getSupportActionBar().setDisplayShowHomeEnabled(true);//使圖標可以顯示
點擊還是沒反應,在網上查找,發現缺少如下代碼
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) //應用程序圖標的id
{
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
終于,返回功能正常使用。