getApplicationContext() 与 Activity.this
程序员文章站
2022-03-05 12:27:11
...
写道
In various bits of Android code I've seen:
public class MyActivity extends Activity {
public void method() {
mContext = this; // since Activity extends Context
mContext = getApplicationContext();
mContext = getBaseContext();
}
}
However I can't find any decent explanation of which is preferable, and under what circumstances which should be used.
Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, would be much appreciated.
I agree that documentation is sparse when it comes to Contexts in Android, but you can piece together a few facts from various sources.
This blog post on the official Google Android developers blog was written mostly to help address memory leaks, but provides some good information about contexts as well:
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.
public class MyActivity extends Activity {
public void method() {
mContext = this; // since Activity extends Context
mContext = getApplicationContext();
mContext = getBaseContext();
}
}
However I can't find any decent explanation of which is preferable, and under what circumstances which should be used.
Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, would be much appreciated.
I agree that documentation is sparse when it comes to Contexts in Android, but you can piece together a few facts from various sources.
This blog post on the official Google Android developers blog was written mostly to help address memory leaks, but provides some good information about contexts as well:
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.
最关键的地方---- 写道
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 got bad window token errors when using application context to Progress Dialog. So i used activity context instead.
-------------------------分割线:上面 的是从网上搜得解释;下面是我遇到的问题--------------------------
写道
官方网站:http://developer.android.com/guide/topics/ui/dialogs.html
Creating a Custom Dialog
第二步:Set the above layout as the dialog's content view and define the content for the ImageView and TextView elements:
按照这个代码来写,会报错误;
如果把上下文 Context mContext = getApplicationContext();
Creating a Custom Dialog
第二步:Set the above layout as the dialog's content view and define the content for the ImageView and TextView elements:
按照这个代码来写,会报错误;
如果把上下文 Context mContext = getApplicationContext();
---改为--- Context mContext = Activity类名.this 就正确。
上一篇: 单机和弱联网游戏内购,白嫖的机会来了
推荐阅读
-
mysql 定时任务的实现与使用方法示例
-
VS2013创建Windows服务与调试服务的图文方法
-
今晚7:30好米不停!A5域名拍卖与您不见不散
-
运营技巧:沟通,运营者与用户沟通过程的距离
-
Mysql数据库中数据表的优化、外键与三范式用法实例分析
-
Swoole协程与传统fpm同步模式比较
-
域名投资的疯狂、逆袭与颠覆!2105年最大的风险是观望
-
abp(net core)+easyui+efcore实现仓储管理系统——ABP WebAPI与EasyUI结合增删改查之八(三十四)
-
详谈C# 图片与byte[]之间以及byte[]与string之间的转换
-
soureTree中如何设置git 用户名与密码 SourceTree提交修改用户详细图文方法