从活动创建新的 menuInflater 或 getMenuInflater() ?

发布于 2024-12-09 17:31:09 字数 841 浏览 0 评论 0原文

我正在片段内创建新的选项菜单,但阅读后 http://developer.android.com/resources/articles/avoiding-memory -leaks.html 这说使用 context-application 比 context-activity 更好,我担心使用 getActivity().getMenuInflater()

所以,实际上哪个更好

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuInflater mInflater = new MenuInflater(getActivity().getApplicationContext());
    mInflater.inflate(R.menu.simple_menu, menu);
}

或者一个调用 Activity

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuInflater mInflater = getActivity().getMenuInflater();
    mInflater.inflate(R.menu.simple_menu, menu);

}

,两者之间有什么区别?或者..两者都是一样的?

谢谢。

I'm creating new option menu inside fragment but after reading
http://developer.android.com/resources/articles/avoiding-memory-leaks.html
which said to it's better to use context-application than context-activity, I'm afraid to use getActivity().getMenuInflater()

So, actually which one better

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuInflater mInflater = new MenuInflater(getActivity().getApplicationContext());
    mInflater.inflate(R.menu.simple_menu, menu);
}

Or the one call activity

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuInflater mInflater = getActivity().getMenuInflater();
    mInflater.inflate(R.menu.simple_menu, menu);

}

and, what's the differences between the two of 'em? or..both are just the same?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

夜深人未静 2024-12-16 17:31:09

他们非常相似。查看 MenuInflator 的源代码,它使用上下文的唯一目的是访问资源文件。所以具体的上下文对于 MenuInflator 来说并不重要。

至于内存泄漏,你引用的文章说

最明显的[避免内存泄漏的方法]是避免转义
上下文超出其自身范围

除非将 MenuInflator(或 Menu)传递给另一个类,否则它包含在活动中并且不会泄漏。

编辑

此外Activity.getMenuInflator()只是new MenuInflator()的一个便捷方法。事实上,这是 Activity 类中的方法:

public MenuInflater getMenuInflater() {
    return new MenuInflater(this);
}

通常最好使用便捷方法,因为它们允许在未来版本中更改底层实现,而无需更改代码。例如,如果将上述方法修改为返回一个缓存的实例,而不是每次创建一个新实例。

They are very similar. Looking through the MenuInflator's source, the only thing it uses the context for is to access the resource files. So the specific context doesn't matter to the MenuInflator.

As for memory leaks, the article you reference says

The most obvious [way to avoid memory leaks] is to avoid escaping the
context outside of its own scope

Unless you pass the MenuInflator (or Menu) to another class then it is contained in the activity and won't be leaked.

EDIT

In addition Activity.getMenuInflator() is just a convenience method for new MenuInflator(). In fact this is the method inside the Activity class:

public MenuInflater getMenuInflater() {
    return new MenuInflater(this);
}

It is usually better to use convenience methods since they allow for the underlying implementation to change in future versions without you having to change your code. For example if the above method is modified to return a cached instance instead of creating a new one each time.

梦一生花开无言 2024-12-16 17:31:09

您应该使用传递给方法 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)MenuInflater 实例(注意第二个参数)
没有真正的区别,因为通常您一使用完它就会“忘记”它,但是如果您已经从参数中获得了一个,为什么还需要创建一个呢?

You should use MenuInflater instance that is passed to the method public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) (notice the 2nd argument)
There's no real difference, because normally you will "forget" it as soon as you finish using it, but why would you need to create one if you already have one from the arguments?

醉城メ夜风 2024-12-16 17:31:09

从此 : 更改

MenuInflater inflater = getMenuInflater();

为:

MenuInflater inflater = getActivity().getMenuInflater();

Change from this :

MenuInflater inflater = getMenuInflater();

To this:

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