onOptionsItemSelected雖然與我的應用程序的工作,我不得不來到碰到過,我應該使用具有得到顯示在整個應用程序菜單的情況下,當用戶點擊菜單按鈕。
所以我用下面的代碼在默認的活動,但後來意識到菜單在活動顯示但不是在所有。顯示菜單中的所有活動,不重寫onCreateOptionsMenu每次
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.del_boy_menu, menu);
//below comented code for changung dynamically
// MenuItem bedMenuItem = menu.findItem(R.id.home);
// bedMenuItem.setTitle("title changed");
// System.out.println("onCreate executed");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
System.out.println("onOptionSelected executed");
switch (item.getItemId()) {
case R.id.home:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(MainDeliveryBoyActivity.this, "Home is Selected", Toast.LENGTH_SHORT).show();
// MenuHomeActivity
startActivity(new Intent(context,MenuHomeActivity.class));
return true;
case R.id.delivered1:
Toast.makeText(MainDeliveryBoyActivity.this, "delivered is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.cancelled:
Toast.makeText(MainDeliveryBoyActivity.this, "cancelled is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.active:
Toast.makeText(MainDeliveryBoyActivity.this, "active is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
所以我的問題是我應該複製並粘貼在所有活動的所有上面的代碼?或者有什麼方法可以跳過這個?
創建BaseActvity類並覆蓋其方法和擴展在所有其他的活動,活動 –