我有两个活动,主要活动和列表活动。我总是希望主要活动上显示一个菜单,因为没有其他地方可去。
主要活动有以下代码:
@Override
public void onOptionsMenuClosed(Menu menu) {
// Nothing else to do, closing the activity.
finish();
}
@Override
public void onResume() {
super.onResume();
openOptionsMenu();
}
当我关闭List Activity时,我的整个应用程序关闭了 - 由于我的MainActivity中的onOptionsMenuClosed方法,我真的很困惑为什么会这样,当ListActivity关闭时我只想回到菜单。
答案 0 :(得分:0)
我遇到了同样的问题并解决了这个问题:
public class StartActivity extends Activity{
private boolean cancel = true;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
openOptionsMenu();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.startmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.Item1){
cancel = false;
Intent intent = new Intent(this, ActivityA.class);
startActivityForResult(intent, 0);
return true;
}
else if(id == R.id.Item2){
cancel = false;
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, 0);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_CANCELED) {
openOptionsMenu();
cancel = true;
}
}
}
@Override
public void onOptionsMenuClosed(Menu menu)
{
super.onOptionsMenuClosed(menu);
if(cancel){
finish();
}
}
}