从基本方法开始 - 选项菜单。
问题:捕获取消(返回)事件的最佳方法是什么?即用户改变主意并击中后退箭头。问题是我想取消暂停我的应用程序线程。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
onPause();
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.preferences:
Intent settingsActivity = new Intent(this, PreferencesActivity.class);
startActivity(settingsActivity);
return true;
case R.id.quit:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
是否有类似@Override的onCancelOptionsMenu ??
答案 0 :(得分:3)
我认为您正在寻找的功能是onOptionsMenuClosed。
答案 1 :(得分:2)
只是onOptionsMenuClosed()我们无法区分用户是否使用后退/菜单按钮取消了菜单,或者是否选择了某个项目。
我们可以使用ie。:
模拟onCancel()事件onOptionsMenuClosed() onOptionsItemSelected() 以及保持状态的布尔值:
private boolean bMenuCancelled = true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId()<= YOUR_MAX_MENU_ITEM && item.getItemId()>= YOUR_MIN_MENU_ITEM)
bMenuCancelled = false;
else
bMenuCancelled = true;
//...
}
@Override
public void onOptionsMenuClosed(Menu menu)
{
super.onOptionsMenuClosed(menu);
if(bMenuCancelled)
//here the menu has been closed by clicking outside the menu
else
//here the menu has been closed by selecting an item
}