context和getApplicationContext()的区别
在android中常常会遇到与context有关的内容
浅论一下 context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder(this); 中,要求传递的 参数就是一个context,在这里我们传入的是this,那么这个this究竟指的是什么东东呢? 这里的this指的是Activity.this,是这个语句所在的Activity的this,是这个Activity 的上下文。网上有很多朋友在这里传入this.getApplicationContext(),这是不对的。 AlertDialog对象是依赖于一个View的,而View是和一个Activity对应的。 于是,这里涉及到一个生命周期的问题,this.getApplicationContext()取的是这个应 用程序的Context,Activity.this取的是这个Activity的Context,这两者的生命周期是不同 的,前者的生命周期是整个应用,后者的生命周期只是它所在的Activity。而AlertDialog应 该是属于一个Activity的,在Activity销毁的时候它也就销毁了,不会再存在;但是,如果传 入this.getApplicationContext(),就表示它的生命周期是整个应用程序,这显然超过了它 的生命周期了。 所以,在这里我们只能使用Activity的this。
new AlertDialog.Builder(getApplicationContext())时发生错误:
E/AndroidRuntime(5844): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
于是查了查:
getApplicationContext() 生命周期是整个应用,应用摧毁它才摧毁 Activity.this的context属于activity ,activity 摧毁他就摧毁
activity.this要返回一个activity,而getApplicationContext()就不一定返回一个activity
getBaseContext() 返回由构造函数指定或setBaseContext()设置的上下文
Activity.this的context 返回当前activity的上下文,属于activity ,activity 摧毁他就摧毁
getBaseContext() 返回由构造函数指定或setBaseContext()设置的上下文
context和getApplicationContext()的区别的更多相关文章
- android中activity.this跟getApplicationContext的区别
转载: http://www.myexception.cn/android/1968332.html android中activity.this和getApplicationContext的区别 在a ...
- Context与ApplicationContext的区别
ApplicationContext并没有这个类,其实更应该叫做:Activity与Application在作为Context时的区别.嗯,的确是这样的,大家在需要Context的时候,如果是在Act ...
- android学习—— context 和 getApplicationContext()
一直看到好多应用里面,随手使用getApplicationContext(),不想说也不乐意说,今天转载一篇文章区分两者的区别: 在android中常常会遇到与context有关的内容 浅论一下con ...
- 【转】context和getApplicationContext()介绍
在android中常常会遇到与context有关的内容,大多都是作为参数在传递,但是它的作用究竟是什么呢 先说它的用法,举个例子 在语句 AlertDialog.Builder builder = n ...
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别
<context:annotation-config> 和 <context:component-scan>的区别 <context:annotation-config& ...
- <context:annotation-config> 跟 <context:component-scan>诠释及区别
<context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注 ...
- Android 开发笔记“context和getApplicationContext”
在android中常常会遇到与context有关的内容 浅论一下context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder( ...
- 转 android学习—— context 和 getApplicationContext()
在android中常常会遇到与context有关的内容 浅论一下context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder( ...
- spring <context:annotation-config> 跟 <context:component-scan>诠释及区别
<context:annotation-config> 和 <context:component-scan>的区别 Difference between <context ...
随机推荐
- LOJ 3094 「BJOI2019」删数——角标偏移的线段树
题目:https://loj.ac/problem/3094 弱化版是 AGC017C . 用线段树维护那个题里的序列即可. 对应关系大概是: 真实值的范围是 [ 1-m , n+m ] :考虑设偏移 ...
- bzoj 4161 Shlw loves matrixI——常系数线性齐次递推
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4161 还是不能理解矩阵…… 关于不用矩阵理解的方法:https://blog.csdn.ne ...
- AtCoder Grand Contest 012 A - AtCoder Group Contest(贪心)
Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement There are 3N participa ...
- thinkphp5 redis使用
参数参考位置:thinkphp\library\think\cache\driver class Redis extends Driver { protected $options = [ 'host ...
- css控制显示超出多少行以后开始出现省略号的写法
display: -webkit-box; display: -moz-box; text-overflow: -o-ellipsis-lastline; text-overflow: ellipsi ...
- 抽象类 抽象方法 abstract
abstract: * abstract修饰类: 抽象类 * > 此类不能被实例化 * > 抽象类中一定要有构造器, 便于子类对象实例时调用(涉及子类对象实例化过程) * > 开发中 ...
- 指针的value
全局变量的默认初始化值为0,char为空字符——'\0',string为"\0 " char c; string s; ; int main() { cout << p ...
- 模拟用户登录含注册——python第8天
print('欢迎登录尚雅梦想python学习系统'.center(30)) print('******' * 8) flag = True while flag: order = input(''' ...
- 后端数据推送-EventSource
服务器发送事件(以下简称SSE)是HTML 5规范的一个组成部分,可以实现服务器到客户端的单向数据通信.通过SSE,客户端可以自动获取数据更新,而不用重复发送HTTP请求.一旦连接建立,“事件”便会自 ...
- W3C、MDN及html常用标签介绍
W3C 万维网(World Wide Web)是作为欧洲核子研究组织的一个项目发展起来的,在那里 Tim Berners-Lee 开发出万维网的雏形.Tim Berners-Lee- 万维网的发明人和 ...