Android:在 Activity 生命周期中何时调用 onCreateOptionsMenu?
我在 onCreate
中放置了几个断点(一个在方法的开头,一个在方法的末尾),还在 onCreateOptionsMenu
的开头放置了一个。首先调用 onCreate
方法,在完成之前调用 onCreateOptionsMenu
。
我正在尝试分离应用程序中的 Fragment
导航代码,因此我有几个对象委托给 onCreateOptionsMenu
,具体取决于应用程序是否在手机上运行/平板电脑(我使用屏幕尺寸来确定这一点,我的大屏幕布局文件有一个我在布局膨胀后检查的视图)。我遇到的问题是,我在 onCreate 中创建这些对象,当我在 onCreateOptionsMenu 中引用该对象时,出现空指针异常。
I put a couple of breakpoints in onCreate
(one at the beginning, and one at the end of the method), and I also put one at the beginning of onCreateOptionsMenu
. The onCreate
method is called first, and before it finishes onCreateOptionsMenu
is called.
I'm trying to separate the Fragment
navigation code in my app, so I have a couple of objects that I delegate onCreateOptionsMenu
to depending on if the app is running on phone/tablet (I'm using screen size to determine this, my layout file for large screens has a View I check for after the layout is inflated). The problem I'm having is, I create these objects in onCreate, and I'm getting a null pointer exception when I reference the object in onCreateOptionsMenu
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在我的例子中,Android 2.3 和 v4 支持库中的
FragmentActivity
生命周期方法调用的顺序如下:In my case on Android 2.3 and with
FragmentActivity
from v4-support library the order of life-cycle methods invoke is following:我发现如果在 onResume() 中我调用
,那么 onCreateOptionsMenu(Menu menu) 之后会被调用 - 根据活动生命周期 (我认为这是正确的术语),如由@tir38表示
I found if in onResume() I call
then onCreateOptionsMenu(Menu menu) is called afterward - as per the activity life cycle (I think that's the correct term here), as indicated by @tir38
在上面的答案中添加,
对于 ICS 和 Honeycomb,onCreateOptionsMenu 在 onCreate 和 onPostCreate 之后调用,而在 Gingerbread 和更早版本中,它在 onCreate 之后但 onPostCreate 之前调用。这是我发现的唯一区别。
Addition in above answer,
In case of ICS and Honeycomb onCreateOptionsMenu is called after onCreate and onPostCreate while in Gingerbread and earlier versions it is called after onCreate but before onPostCreate. Thats the only difference I found.
根据我的经验,来自支持 v7
onCreateOptionsMenu()
的ActionBarActivity
在onCreate()
setContentView() 方法中调用> 它出现在 4.1.1 上。但在 4.4 上,另一个故事
onCreateOptionMenu()
在onCreate()
之后调用。我也不知道可能是紧接着,也许不是。但这是事实。我没有在其他版本上进行测试,但 4.1.1 是第一个我在初始化顺序方面遇到问题的版本。In my experience
ActionBarActivity
from support v7onCreateOptionsMenu()
called insetContentView()
method in the middle ofonCreate()
it is appear on 4.1.1.But on 4.4 another story
onCreateOptionMenu()
called afteronCreate()
. Also I don't know it may be immediately after, maybe not. But is fact after. I didn't test on other versions but 4.1.1 is first where I had have a trouble with init order.我建议在片段中创建一个回调函数,以避免 onResume() 和 onCreateOptionsMenu() 出现计时问题。
执行以下操作对我来说是完美的:
例子:
i suggest to create a callback-function in your fragment to avoid timing issues with onResume() and onCreateOptionsMenu().
doing the following works flawless for me:
example:
在具有官方蜂巢式操作栏的设备和应用程序上也是如此。如果没有操作栏,则在用户调用菜单(通常是按 MENU 按钮)之前不应调用
onCreateOptionsMenu()
。一旦冰淇淋三明治发货,该测试很快就会中断。据我所知,ICS 手机将有操作栏(尽管可能没有系统栏)。
That will be true on devices and apps with an official Honeycomb-style action bar. If there is no action bar,
onCreateOptionsMenu()
should not get called until the user calls up the menu, typically by pressing the MENU button.That test will break very shortly, once Ice Cream Sandwich ships. From what I can tell, ICS phones will have action bars (though perhaps not system bars).