Android:在 Activity 生命周期中何时调用 onCreateOptionsMenu?

发布于 2024-12-09 02:27:39 字数 404 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

丿*梦醉红颜 2024-12-16 02:27:40

在我的例子中,Android 2.3 和 v4 支持库中的 FragmentActivity 生命周期方法调用的顺序如下:

07-18 18:29:21.629  20183-20183/? I/onCreate:
07-18 18:29:21.719  20183-20183/? I/onStart: 
07-18 18:29:21.719  20183-20183/? I/onResume: 
07-18 18:29:21.739  20183-20183/? I/onCreateOptionsMenu:

In my case on Android 2.3 and with FragmentActivity from v4-support library the order of life-cycle methods invoke is following:

07-18 18:29:21.629  20183-20183/? I/onCreate:
07-18 18:29:21.719  20183-20183/? I/onStart: 
07-18 18:29:21.719  20183-20183/? I/onResume: 
07-18 18:29:21.739  20183-20183/? I/onCreateOptionsMenu:
零時差 2024-12-16 02:27:40

我发现如果在 onResume() 中我调用

invalidateOptionsMenu();

,那么 onCreateOptionsMenu(Menu menu) 之后会被调用 - 根据活动生命周期 (我认为这是正确的术语),如由@tir38表示

@Override
public void onResume() {
    super.onResume();
    invalidateOptionsMenu();
}

I found if in onResume() I call

invalidateOptionsMenu();

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

@Override
public void onResume() {
    super.onResume();
    invalidateOptionsMenu();
}
眼眸里的快感 2024-12-16 02:27:40

在上面的答案中添加,
对于 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.

深居我梦 2024-12-16 02:27:40

根据我的经验,来自支持 v7 onCreateOptionsMenu()ActionBarActivityonCreate()setContentView() 方法中调用> 它出现在 4.1.1 上。

但在 4.4 上,另一个故事 onCreateOptionMenu()onCreate() 之后调用。我也不知道可能是紧接着,也许不是。但这是事实。我没有在其他版本上进行测试,但 4.1.1 是第一个我在初始化顺序方面遇到问题的版本。

In my experience ActionBarActivity from support v7 onCreateOptionsMenu() called in setContentView() method in the middle of onCreate() it is appear on 4.1.1.

But on 4.4 another story onCreateOptionMenu() called after onCreate(). 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.

策马西风 2024-12-16 02:27:40

我建议在片段中创建一个回调函数,以避免 onResume() 和 onCreateOptionsMenu() 出现计时问题。

执行以下操作对我来说是完美的:

  1. 创建片段并将其添加到您的活动中,
  2. 在您的活动中留下对此片段的引用,在您的片段中
  3. 创建一个公共方法 doSomethingWithTheMenu(),
  4. 当调用 onCreateOptionsMenu(Menu menu) 时,从您的活动中调用此方法。

例子:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (this.myFragment != null) {
        this.myFragment.doSomethingWithTheMenu(menu);
    }
    return true;
}

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:

  1. create and add your fragment to your activity
  2. leave a reference of this fragment in your activity
  3. create a public method doSomethingWithTheMenu() in your fragment
  4. call this method from within your activity when onCreateOptionsMenu(Menu menu) is called.

example:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (this.myFragment != null) {
        this.myFragment.doSomethingWithTheMenu(menu);
    }
    return true;
}
遗弃M 2024-12-16 02:27:39

首先调用 onCreate 方法,在完成之前调用 onCreateOptionsMenu。

在具有官方蜂巢式操作栏的设备和应用程序上也是如此。如果没有操作栏,则在用户调用菜单(通常是按 MENU 按钮)之前不应调用 onCreateOptionsMenu()

(我使用屏幕尺寸来确定这一点,我的大屏幕布局文件有一个我在布局膨胀后检查的视图)

一旦冰淇淋三明治发货,该测试很快就会中断。据我所知,ICS 手机将有操作栏(尽管可能没有系统栏)。

The onCreate method is called first, and before it finishes onCreateOptionsMenu is called.

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.

(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)

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文