从活动创建新的 menuInflater 或 getMenuInflater() ?
我正在片段内创建新的选项菜单,但阅读后 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他们非常相似。查看 MenuInflator 的源代码,它使用上下文的唯一目的是访问资源文件。所以具体的上下文对于 MenuInflator 来说并不重要。
至于内存泄漏,你引用的文章说
除非将 MenuInflator(或 Menu)传递给另一个类,否则它包含在活动中并且不会泄漏。
编辑
此外
Activity.getMenuInflator()
只是new MenuInflator()
的一个便捷方法。事实上,这是 Activity 类中的方法:通常最好使用便捷方法,因为它们允许在未来版本中更改底层实现,而无需更改代码。例如,如果将上述方法修改为返回一个缓存的实例,而不是每次创建一个新实例。
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
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 fornew MenuInflator()
. In fact this is the method inside the Activity class: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.
您应该使用传递给方法
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
的MenuInflater
实例(注意第二个参数)没有真正的区别,因为通常您一使用完它就会“忘记”它,但是如果您已经从参数中获得了一个,为什么还需要创建一个呢?
You should use
MenuInflater
instance that is passed to the methodpublic 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?
从此 : 更改
为:
Change from this :
To this: