在android中常常会遇到与context有关的内容,特写下这篇blog,浅论一下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。
分享到:
相关推荐
大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml...
php-sebastian-recursion-context3-3.0.0-1.el7.remi.noarch.rpm
org.springframework.context.support-3.1.0.M1.jar包,无毒下载
org.springframework.context_3.0.5.release.jar.zip
赠送jar包:spring-context-5.2.15.RELEASE.jar; 赠送原API文档:spring-context-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
spring-context-support-4.0.5.RELEASE.jar
spring-context-1.2.8.jar, spring-context-1.2.9.jar, spring-context-2.0-m2.jar, spring-context-2.0.1.jar, spring-context-2.0.2.jar, spring-context-2.0.4.jar, spring-context-2.0.6.jar, spring-context-...
赠送jar包:spring-context-support-5.2.0.RELEASE.jar; 赠送原API文档:spring-context-support-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-context-...人性化翻译,文档中的代码和结构保持不变,注释和说
spring-context-support-3.2.0.RELEASE.jar
这个包里有context等重要类的源代码
赠送jar包:spring-context-support-5.0.5.RELEASE.jar; 赠送原API文档:spring-context-support-5.0.5.RELEASE-javadoc.jar;...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放
赠送jar包:spring-context-support-5.2.7.RELEASE.jar; 赠送原API文档:spring-context-support-5.2.7.RELEASE-javadoc.jar;...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放
Android Context使用例子.
java解决org.springframework.web.context.ContextLoaderListener
org.springframework.context-3.1.1.RELEASE.jar java 开发专用
spring-context-3.0.2.release.jar.zip
org.springframework.context.support-3.0.5.RELEASE.jar
赠送jar包:spring-context-5.0.8.RELEASE.jar; 赠送原API文档:spring-context-5.0.8.RELEASE-javadoc...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。
将我的博客文章(https://blog.csdn.net/IT_interest/article/details/122973985)中的内容用项目实现出来,功能为:使用webpack中的require.context功能,批量导入本地图片,并且可根据需要支持多格式图片导入。...
org.springframework.context.support-3.0.0.M3.jar