关闭

Android getActivity(), getContext(), getApplication()和getApplicationContext()

标签: androidContextApplicatiothis
2785人阅读 评论(0) 收藏 举报
分类:
  1. getActivity():
    This method gives the context of the Activity. You can use it is like the yourActivity.this. The method getActivity() is normally used in fragments to get the context of the activity in which they are inserted or inflated.

  2. getContext():
    Returns the context the view is currently running in. Usually the currently active Activity. getContext() is not defined in an Activity. It’s used in a View (or View subclass) to get a reference to the enclosing context (an Activity).

Activity is a subclass of Context.

When you call getActivity() you get an Activity which is a Context as well. But when you call getContext you will get a Context which might not be an Activity.

When using newer version of Support Library, when Fragment is not hosted by an Activity you can get different objects when calling getActivity() and getContext().

3.getApplication():
getApplication() is available to Activity and Services only. Although in current Android Activity and Service implementations, getApplication() and getApplicationContext() return the same object, there is no guarantee that this will always be the case (for example, in a specific vendor implementation). So if you want the Application class you registered in the Manifest, you should never call getApplicationContext() and cast it to your application, because it may not be the application instance (which you obviously experienced with the test framework).

4.getApplicationContext():
Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.

What’s the difference between the various methods to get a Context?

 public class MyActivity extends Activity {
    public void method() {
       mContext = this;    // since Activity extends Context
       mContext = getApplicationContext();
       mContext = getBaseContext();
    }
 }

http://stackoverflow.com/questions/1026973/whats-the-difference-between-the-various-methods-to-get-a-context

0
1
查看评论

android学习—— context 和 getApplicationContext()

在android中常常会遇到与context有关的内容 浅论一下context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder(this); 中,要求传递的 参数就是一个context,在这里我们传入的是this,那么这个th...
  • janronehoo
  • janronehoo
  • 2012-03-13 14:04
  • 145855

Android Context 上下文 你必须知道的一切

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40481055,本文出自:【张鸿洋的博客】本文大多数内容翻译自:http://www.doubleencore.com/2013/06/context/  我重新...
  • lmj623565791
  • lmj623565791
  • 2015-01-05 09:21
  • 165787

Android中的getApplication()、getApplicationContext的区别与用法

getApplication()和getApplicationContext()方法的区别和使用。
  • u014665856
  • u014665856
  • 2017-05-16 23:26
  • 1989

熟悉Android Context与GetApplicationContext的区别及使用,能够解决内存泄漏问题

原文地址: http://blog.csdn.net/lixuce1234/article/details/62039807 1、Context概念 其实一直想写一篇关于Context的文章,但是又怕技术不如而误人子弟,于是参考了些资料,今天准备整理下写出来,如有不足,请指出,参考...
  • u012900118
  • u012900118
  • 2017-05-18 00:11
  • 231

安卓中为了获取context的方法和区别(getContext,getActivity,this,mainActivity.this等)

安卓中为了获取context的方法和区别(getContext,getActivity,this,mainActivity.this等)
  • wyl_1994
  • wyl_1994
  • 2016-06-07 18:06
  • 8065

Andriod getContext和getActivity

MainActivity.this:表示MainActivity对象,一般用在内部类中指示外面的this,如果在内部类直接用this,指示的是内部类本身。因为MainActivity继承Activity,而Activity继承Context,所以它也可以用来提供Activity Contex; th...
  • myth13141314
  • myth13141314
  • 2017-03-14 16:31
  • 1543

context和getApplicationContext()介绍

在android中常常会遇到与context有关的内容 浅论一下context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder(this); 中,要求传递的 参数就是一个context,在这里我们传入的是this,那么这个th...
  • jason0539
  • jason0539
  • 2013-07-31 06:34
  • 19246

Android之 getActivity()

知识点:会持续更新关于fragment类的一些常用方法解释 1、 getActivity()的解释;   getActivity() /** 返回一个和此fragment绑定的FragmentActivity或者其子类的实例。相反,如果此fragment绑定的是一个con...
  • qq_16628781
  • qq_16628781
  • 2017-03-09 18:02
  • 1929

解决Android里getActivity()为空的问题

在开发项目中有时会碰到getActivity函数为空的情况,    导致apk空指针崩溃问题,  看代码逻辑没有明显的原因。        如果看Activity.java、FragmentActiv...
  • brycegao321
  • brycegao321
  • 2016-07-29 10:01
  • 6113

关于getContext()、getApplication()、getApplicationContext()、getActivity()的区别

关于getContext()、getApplication()、getApplicationContext()、getActivity()的区别: (1).getContext():获取到当前对象的上下文。 (2).getApplication():获得Application的对象 (3).g...
  • wpj1119245135
  • wpj1119245135
  • 2016-04-14 11:45
  • 4540
    个人资料
    • 访问:80462次
    • 积分:1395
    • 等级:
    • 排名:千里之外
    • 原创:67篇
    • 转载:2篇
    • 译文:0篇
    • 评论:11条
    文章分类
    最新评论