`

Android 之Context、Activity、Application之间有什么区别

 
阅读更多
引用
http://liuyun025.iteye.com/blog/1139970


在语句 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 activity跳转 Application Context实现共享数据

    涉及:activity跳转、intent使用、startActivityForResult/onActivityResult,着重讲解通过来Application Context实现共享数据

    谈谈Android里的Context的使用

    大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml...

    Introduction.to.Android.Application.Development(4th,2013.12) pdf

    Adding Logging Support to Your Android Application 83 Adding Some Media Support to Your Application 84 Adding Location-Based Services to Your Application 88 Debugging Your Application on Hardware 90 ...

    Android编程中context及全局变量实例详解

    今天在研究context的时候,对application和activity context有了一定的了解,下面是从网上复制过来的资料 Application context和Activity context的区别: 这是两种不同的context,也是最常见的两种。第一种中...

    Android代码-Android_Learning_Notes

    3、Android03--Context和Application 4、Android04--Android服务 5、Android05--Android服务通信 6、Android06--Android广播接收器 7、Android07--Android日志系统 8、Android08--Android权限系统 用户界面 9、...

    android基础教程之context使用详解

    在android中有两种context,一种是application context,一种是activity context,通常我们在各种类和方法间传递的是activity context。 区别联系: 代码如下:public class MyActivity extends Activity { public ...

    Android代码-AndroidHookStartActivity

    1.application标签里配置一个壳Activity 2.注册一下其中this为context AMSHookUtil.hookStartActivity(this); 3.以后就可以按照标准的Intent启动为那些未被注册的Activity。 Intent intent = new Intent...

    Android开发之activity的生命周期详解

    本文实例讲述了Android activity的生命周期。分享给大家供大家参考,具体如下: activity类处于android.app包中,继承体系如下: 1.Java.lang.Object 2.android.content.Context 3.android.app.ApplicationContext 4...

    Android代码-support-application

    support-application is a library which can get the information about the app like application, applicationContext, classloader, appName, versionName, versionCode, isDebugAble without context. ...

    避免 Android中Context引起的内存泄露

    常用的有Activity的Context还是有Application的Context。Activity用来展示活动界面,包含了很多的视图,而视图又含有图片,文字等资源。在Android中内存泄露很容易出现,而持有很多对象内存占用的Activity更加容易...

    Android编程实现全局获取Context及使用Intent传递对象的方法详解

    本文实例讲述了Android编程实现全局获取Context及使用Intent传递对象的方法。分享给大家供大家参考,具体如下: 一、全局获取 Context Android 开发中很多地方需要用到 Context,比如弹出 Toast、启动活动、发送广播...

    android检查内存泄露

    android检查内存泄露和用法 <!--==============================================================================--> android:name="com.squareup.leakcanary.internal.HeapAnalyzerService" android:enabled...

    新版Android开发教程.rar

    ----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...

    ActivityShareData.rar

    Android 多个Activity之间共享类(数据)——Application Context

    详解Android中的Context抽象类

    关于Context我们首先应该知道: ...于是,我们可以利用该Context对象去构建应用级别操作(application-level operations) 。 一、Context相关类的继承关系 相关类介绍: Context类路径: /frameworks/bas

    Android功能代码

    Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_SETTINGS);//管理应用程序界面 context.startActivity(intent); 或者用以下方法(3.0以前的版本可以用此方法) Intent intent = new Intent...

    安卓区分几种不同的Context的区别

    可以参考这篇文章 ...Context |— ContextWrapper |— —Application |— —ContextThemeWrapper ...Application的Context对于Activity而言没有返回栈 Context一般用来access system level resources (getSystemSer

    Android项目设计与开发:服务的概念.ppt

    嵌入式Android项目设计与开发 第八章 服务 —— 服务的概念 服务的概念 服务(Service)是Android中的四大组件之一,它能够长期在后台运行且不提供用户界面。即使用户切到另一应用程序,服务仍可以在后台运行。 服务...

    android中用getApplicationContext()会不会避免某些内存泄漏问题?

    作者:Hewi ...来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。...首先,Activity的Context和Application的Context肯定不是一个东西,一个是当前活动的 Context,它的生命周期仅限

    打开附件.java

    import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.widget.Toast; import java.io.File; /** * Created by niuyouquan ...

Global site tag (gtag.js) - Google Analytics