onCreateOptionsMenu()调用超onCreateOptionsMenu

2023-09-04 10:00:59 作者:此人丶卟①般

我要创建应用程序的 OptionsMenu 。我发现几个例子吧,但使用不同的地方,每个人都打电话 super.onCreateOptionMenu() onCreateOptionsMenu()方法。

I'm creating application with OptionsMenu. I found few examples with it, but everyone using different place where to call super.onCreateOptionMenu() in onCreateOptionsMenu() method.

不同的方式列表:

@Override // without super
public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.mymenu, menu);
  return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.mymenu, menu);
  return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.mymenu, menu);
  return super.onCreateOptionsMenu(menu);
}

我应该使用什么?

What should I use?

推荐答案

这取决于你想要做什么。第一个例子将会把你的菜单,只有你的菜单。第二个,将新增首超一流的菜单。最后一个将首先添加你的菜单。但是,请记住,菜单也有一个订单场,这将在渲染时予以考虑。

It depends on what you want to do. First example will place your menu and only your menu. Second one, will add first super class menu. Last one will add your menu first. But, keep in mind that menus also have an order field, which will be taken into account at render time.

假设你正在扩展已经有一个菜单的活动,但你不希望出现的菜单,但一个又一个。在这种情况下,你可以使用第一种方法。

Let's say you are extending an activity that already has a menu, but you do not want that menu to appear but another one. In that case you would use first approach.

又如:要扩展,有一个菜单的活动,并要添加另一个菜单。在这种情况下,你可以使用第二或最后一种方法。

Another example: you are extending an activity that has a menu, and you want to add another menu. In that case you could use either second or last approach.