android中的getActivity()和getApplicationContext()有什么区别? [英] What is the difference between getActivity() and getApplicationContext() in android?

查看:71
本文介绍了android中的getActivity()和getApplicationContext()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上下文中使用方法时,总是会混淆使用哪个方法和何时使用,即getActivity()或getApplicationContext().请帮我解决这个问题.预先感谢.

解决方案

getSystemService() Context 上的一种方法. Fragment 不继承自 Context .活动可以.您可以在 Fragment 上调用 getActivity(),以获得在其上调用 getSystemService() Activity ./p>

When using methods on context, I always get a confusion which one to use and when, namely, getActivity() or getApplicationContext() . Please help me solve this. Thanks in advance.

解决方案

getSystemService() is a method on Context. Fragment does not inherit from Context. Activity does. You can call getActivity() on your Fragment to obtain an Activity on which to call getSystemService().

这篇关于android中的getActivity()和getApplicationContext()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆