与 onCreateOptionsMenu 方法不同的行为
我在这篇文章中发现了一些让我困惑的地方 http://developer.android.com/guide/topics/ui/menus.html
下面是一段简单的代码和输出 当我在 android 3.0
- 和 2. 上启动应用程序时,一旦创建活动,系统就会执行。
按下菜单并选择菜单项时,将打印第 3 行和第 4 行。
问题是第 5 行和 buttonHandler 方法,该方法调用 invalidateOptionsMenu (); 调用的结果是第 6 行和第 7 行
为什么系统在 onPrepareOptionsMenu 之前调用 onCreateOptionsMenu 即使他们已经写了
在 Android 3.0 及更高版本上,当您想要更新菜单时,必须调用 invalidateOptionsMenu(),因为菜单始终处于打开状态。然后系统将调用 onPrepareOptionsMenu(),以便您可以更新菜单项。
在这种情况下,每次 invalidateOptionsMenu() 之后都会调用 onCreateOptionsMenu, 但是当我在 android 2.3 上启动应用程序时, onCreateOptionsMenu 仅被调用一次。
1 信息/System.out(382):onCreateOptionsMenu
2 INFO/System.out(382):onPrepareOptionsMenu
3 INFO/System.out(382):onPrepareOptionsMenu
4 信息/System.out(382):onOptionsItemSelected
5 INFO/System.out(382): 按钮处理程序
6 信息/System.out(382):onCreateOptionsMenu
7 信息/System.out(382):onPrepareOptionsMenu
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
System.out.println("onCreateOptionsMenu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu) {
System.out.println("onPrepareOptionsMenu");
return super.onPrepareOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println("onOptionsItemSelected");
return super.onOptionsItemSelected(item);
}
public void buttonHandler(View v){
System.out.println("buttonHandler");
invalidateOptionsMenu();
}
I found something confused for me in this article
http://developer.android.com/guide/topics/ui/menus.html
Below is simple piece of code and output
When I start application on android 3.0
- and 2. line system executed as soon as the activity is created
The 3. and 4. lines were printed when menu was pressed and menu item was chosen
Probem is line 5 and buttonHandler method, that method calls invalidateOptionsMenu();
and result of that calls is line 6 and 7
Why system calls onCreateOptionsMenu before onPrepareOptionsMenu even they have written
On Android 3.0 and higher, you must call invalidateOptionsMenu() when you want to update the menu, because the menu is always open. The system will then call onPrepareOptionsMenu() so you can update the menu items.
In this case onCreateOptionsMenu is called every time after invalidateOptionsMenu(),
but when I start application on android 2.3 onCreateOptionsMenu was called only once.
1 INFO/System.out(382): onCreateOptionsMenu
2 INFO/System.out(382): onPrepareOptionsMenu
3 INFO/System.out(382): onPrepareOptionsMenu
4 INFO/System.out(382): onOptionsItemSelected
5 INFO/System.out(382): buttonHandler
6 INFO/System.out(382): onCreateOptionsMenu
7 INFO/System.out(382): onPrepareOptionsMenu
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
System.out.println("onCreateOptionsMenu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu) {
System.out.println("onPrepareOptionsMenu");
return super.onPrepareOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println("onOptionsItemSelected");
return super.onOptionsItemSelected(item);
}
public void buttonHandler(View v){
System.out.println("buttonHandler");
invalidateOptionsMenu();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是一个文档错误。我希望在
invalidateOptionsMenu()
之后调用onCreateOptionsMenu()
。That is probably a documentation bug. I would expect
onCreateOptionsMenu()
to be called afterinvalidateOptionsMenu()
.