public boolean onOptionsItemSelected(MenuItem item) {
int itemThatWasClickedId = item.getItemId();
if (itemThatWasClickedId == R.id.action_search) {
Context context = MainActivity.this;
String textToShow = "Search clicked";
Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item); //這樣的返回有什麼意思呢
}
}
onOptionsItemSelected返回值作用
根據API說明可以知道:
boolean Return false to allow normal menu processing to proceed, true to consume it here.
onOptionsItemSelected的返回值意思:
true表示該方法執行完畢後,點擊事件不會再向下一個事件處理方法傳遞了。
false表示執行完該方法後,點擊事件繼續向下傳遞。