`
wiseideal
  • 浏览: 444485 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Context的区别

 
阅读更多
引用

In a regular Android application, you usually have two kinds of Context, Activity and Application.

Reading the article a little bit further tells about the difference between to the two and why you might want to consider using the application Context (Activity.getApplicaitonContext()) rather than using the Activity context ("this"). Basically the Application context is associated with the Applicaiton and will always be the same throughout the life cycle of you app, where as the Activity context is associated with the activity and could possible be destroyed many times as the activity is destroyed during screen orientation changes and such.

I couldn't find really anything about when to use getBaseContext() other than a for a one liner from Dianne Hackborn, one of the Google engineers working on the Android SDK:

Don't use getBaseContext(), just use the Context you have.

That was from a post on the android-developers newsgroup, you may want to consider asking your question there as well because a handful of the people working on Android actual monitor that newsgroup and answer questions.

So overall it seems preferable to use the global application context when possible.


I got bad window token errors when using application context to Progress Dialog. So i used activity context instead.


在一般android程序中,通常有两种类型的Context:Activity类型以及Application类型。

通过更深的了解,你也许会在使用Application类型的Context时比使用Activity类型的更小心。基本上,Application类型的Context会伴随程序的整个生命周期,而Activity类型的Context则与Activity的状态相关,它会随着Activity的消亡而消亡。例如当屏幕方向发生变化时。


我找不到更多关于getBaseContext()的相关用法,除了google Android SDK工程师 Dianne Hackborn的一句话
不要使用getBaseContext(),使用你手头上的Context。

这句话来自android-developers 新闻组,许许多多的android开发者都在关注着新闻组,你也可以在那里提出自己的问题,有人会回答。

所以总的来说最好还是尽可能的使用Application类型的context。
我在使用ProgressDialog的时候使用 Application类型的Context 遇到了 bad window token errors。然后我用Activity类型的Context替代
分享到:
评论

相关推荐

    从源码解析Context思维导图

    详细总结了Context是什么?Android系统中Context的继承关系,不同Context源码分析,Context应用场景,getApplication和getApplicationContext区别。

    Multiple PDP context (高通文档)

    高通(Qualcomm)的关于多个PDP Context业务模型的描述,清楚的讲述了Primary PDP context, Secondary PDP Context, Multiple Primary PDP Context等的概念及区别。有价值的资料

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

    Context |— ContextWrapper |— —Application |— —ContextThemeWrapper |— — — —Activity |— —Service |— — — IntentService Application的Context对于Activity而言没有返回栈 Context一般用来access ...

    spring配置中<context-param> 和<init-param>的 区别

    &lt;context-param&gt; 和的 区别代码 博文链接:https://xhy0422.iteye.com/blog/46319

    init-parameter和context-parameter区别

    init-parameter和context-parameter区别

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

    Application context和Activity context的区别: 这是两种不同的context,也是最常见的两种。第一种中context的生命周期与Application的生命周期相关的,context随着Application的销毁而销毁,伴随application的一生...

    Android Context与GetApplicationContext的区别及使用1

    1、Context概念其实一直想写一篇关于Context的文章,但是又怕技术不如而误人子弟,于是参考了些资料,今天准备整理下写出来,如有不足,请指出,参考资料会

    GoLang之使用Context控制请求超时的实现

    起因   之前接触了一个需求:提供一个接口,这个接口有一个超时时间,如果超时了返回超时异常;这个接口中调用其他的接口,如果调用超时了,所有请求全部结束。   在这个接口中,我使用了go协程去...两个区别在于T

    <context-param>与<init-param>的区别与作用

    NULL 博文链接:https://wy649898543.iteye.com/blog/1833263

    android基础教程之context使用详解

    区别联系: 代码如下:public class MyActivity extends Activity { public void method() { mContext = this; // since Activity extends Context mContext = getApplicationContext(); mContext = get

    Android Intent和Intent Filter详解

     将intent对象传给 Context.sendBroadcast(), Context.sendOrderedBroadcast(),或者Context.sendStickyBroadcast()等广播方法,则它们被传给 broadcast receiver.  在上述三种情况下, android系统会自己找到合适的...

    tomcat自定义Web部署文件中docBase和workDir的区别介绍

    本文主要介绍的是tomcat自定义Web部署文件中docBase和workDir的区别,下面话不多说,直接来看详细介绍。 首先看这段tomcat配置文件: &lt;Context path=/web reloadable=false docBase=D:\CAPRuntime\src\main\webapp ...

    Android powermanger wakelock

    这两种机制的区别在于,前者无论 acquire() 了多少次,只要通过一次 release()即可解锁。而后者正真解锁是在( --count == 0 )的时候,同样当 (count == 0) 的时候才会去申请加锁。所以 PowerManager.WakeLock 的...

    react-context-example

    介绍这个项目提供了一个如何使用React的上下文API的示例。... 另外,请参见此内容以了解上下文和Redux之间的区别。语境上下文需要一个上下文,一个生产者。 和一个消费者。 请参阅src / components / App.jsx。

    关于XQUERY的误解及其解答

    解答了一些关于XQUERY的问题,解答了一些关于XQUERY的问题,解答了一些关于XQUERY的问题。

    maven相关资料

    二、ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]和FileSystemXmlApplicationContext的区别 classpath:前缀是不需要的,默认就是指项目的classpath路径下面; 如果要使用绝对路径,...

    ContextCapture(Smart3d)中文用户手册

    ContextCapture中文用户手册2016年12月16日版。和最新的4.4.9界面有轻微区别,但是足够用了,是个入门的好文档。

Global site tag (gtag.js) - Google Analytics