Difference between getContext() , getApplicationContext() , getBaseContext()

1. http://stackoverflow.com/questions/22966601/what-is-different-between-mainactivity-this-vs-getapplicationcontext/22967165#22967165

There are two types of Context:

Application context is associated with the application and will always be same throughout the life of application -- it does not change. So if you are using Toast, you can use application context or even activity context (both) because toast can be displayed from anywhere with in your application and is not attached to a specific window. But there are many exceptions, one exception is when you need to use or pass the activity context.

Activity context is associated with to the activity and can be destroyed if the activity is destroyed -- there may be multiple activities (more than likely) with a single application. And sometimes you absolutely need the activity context handle. For example, should you launch a new activity, you need to use activity context in its Intent so that the new launching activity is connected to the current activity in terms of activity stack. However, you may use application‘s context too to launch a new activity but then you need to set flag Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.

Let‘s consider some cases:

MainActivity.this refers to the MainActivity context which extends Activity class but the base class (activity) also extends Context class, so it can be used to offer activity context.

getBaseContext() offers activity context.

getApplication() offers application context.

getApplicationContext() also offers application context.

For more information please check this link.

2. http://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and/10641257#10641257

  • View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity.
  • 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.
  • ContextWrapper.getBaseContext(): If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().
时间: 2024-10-11 13:16:31

Difference between getContext() , getApplicationContext() , getBaseContext()的相关文章

getContext() , getApplicationContext() , getBaseContext()的区别

1.View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity. 当前Activity的上下文 2.Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are ru

绝对让你理解Android中的Context

这个问题是StackOverFlow上面一个热门的问题What is Context in Android? 整理这篇文章的目的是Context确实是一个很抽象的东西,我们在项目中随手都会用到它,可是很多人根本不理解它到底是干什么的,这篇文章还会添加Context in Andorid – INSIGHT的翻译,绝对让读者理解Context的意义. 老规矩,作者提出的问题: 在Android中,Context到底是个什么鬼东西,它到底是干嘛使得,我读了很多篇文档,然而并不能清除的理解它的含义.

Android应用Activity、Dialog、PopWindow、Toast窗体加入机制及源代码分析

[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重劳动成果] 1 背景 之所以写这一篇博客的原因是由于之前有写过一篇<Android应用setContentView与LayoutInflater载入解析机制源代码分析>.然后有人在文章以下评论和微博私信中问我关于Android应用Activity.Dialog.PopWindow载入显示机制是咋回事,所以我就写一篇文章来分析分析吧(本文以Android5.1.1 (API 22)源代码为基础分析),以

如何在Fragment中获取context

文章转载自http://blog.csdn.net/demonliuhui/article/details/51511136 这里仅供自己学习参考: Context,中文直译为"上下文",SDK中对其说明如下: 1.它描述的是一个应用程序环境的信息,即上下文. 2.该类是一个抽象(abstract class)类,Android提供了该抽象类的具体实现类(后面我们会讲到是ContextIml类). 3.通过它我们可以获取应用程序的资源和类,也包括一些应用级别操作,例如:启动一个Acti

Android 仿360桌面小人

首先自定义FloatsWindowView,用于显示动画小人. import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.graphics.drawable.AnimationDrawable; import android.util.AttributeSet; import android.util.DisplayMe

Android应用Activity、Dialog、PopWindow、Toast窗口添加机制及源码分析

1  背景 之所以写这一篇博客的原因是因为之前有写过一篇<Android应用setContentView与LayoutInflater加载解析机制源码分析>, 然后有人在文章下面评论和微博私信中问我关于Android应用Activity.Dialog.PopWindow加载显示机制是咋回事,所以我就写一 篇文章来分析分析吧(本文以Android5.1.1 (API 22)源码为基础分析),以便大家在应用层开发时不再迷糊. PS一句:不仅有人微博私信我这个问题,还有人问博客插图这些是用啥画的,这

Android Application 类共享全局数据

android系统会为每个程序运行时创建一个Application类的对象且仅创建一个,所以Application可以说是单例模式的一个类.且application对象的生命周期是整个程序中最长的,它的生命周期就等于这个程序的生命周期.因为它是全局的单例的,所以在不同的Activity,Service中获得的对象都是同一个对象.所以通过Application来进行一些,数据传递,数据共享 等,数据缓存等操作,代码如下: package com.example.five; import andro

Toast分析——实现自己的Toast

android 4.0以后,新增了一个功能:关闭某个应用发出的通知.Toast等.详细操作为:打开应用安装列表.找到要屏蔽的应用(长按通知,点击弹出的"应用信息",就可以跳到应用信息界面),把同意推送消息(显示通知)取消就可以. 产品发现这个功能之后,果断要求屏蔽.能力有限,不知道怎样破通知的屏蔽.自己实现一个Toast还是小case的-- Toast的实现非常快想到两种方案.Dialog和浮窗(WindowManager).Dialog怀疑代价可能比較大,因此没有去尝试.直接来看浮窗

【腾讯御安全】基于代理Application机制的Anddroid应用加壳方法

壳是指一个程序的外面再包裹上另外一段代码,保护里面的代码不被非法修改或反编译的程序.它们一般都先于程序运行,拿到控制权,然后完成它们保护软件的任务.对于Android APP来说,所有的实现逻辑都集成于DEX文件中,DEX文件是一个APP的核心所在,因此保护DEX文件被逆向或者修改尤其重要.Android应用的DEX加壳过程如下: 尽管上述DEX加壳过程中,关于如何对DEX进行加密或者利用其它方法对其进行处理的方法有很多,但是通过自定义DexClassLoader替换原DexClassLoade