`
ylzyd12345
  • 浏览: 118537 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
博客专栏
713488f4-d0a9-3f35-997f-199fb0cb3563
玩懂Log,打开Andro...
浏览量:10151
社区版块
存档分类
最新评论

getApplicationContext() 与 Activity.this

阅读更多
 写道
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.

 最关键的地方---- 写道

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();
---改为--- Context mContext = Activity类名.this 就正确。
分享到:
评论
1 楼 twlkyao 2013-12-28  
这个貌似是StackOverflow上的http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context

相关推荐

    图片选择器

    Intent intent=new Intent(Main2Activity.this,CCwantPhotoBrowserActivity.class); intent.putExtra("CCwantPhotoList",(Serializable) mData); intent.putExtra("CCwantPhotoPosition",(Serializable)position)...

    Android 文件读写源码

    Button button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new ButtonClickListener()); } private final class ButtonClickListener implements View.OnClickListener{ ...

    谈谈Android里的Context的使用

    大家好,今天给大家分享一下Android... aContext = getApplicationContext(); } /**获取Context. * @return */ public static Context getContext(){ return aContext; } } 第5步:运行上述工程效果如下:

    安卓总结的文档

    etUserName=(EditText) this.findViewById(R.id.etUserName); etPassword=(EditText) this.findViewById(R.id.etPassword); btnLogin=(Button) this.findViewById(R.id.btnLogin); btnReset=(Button) this....

    android 百度地图 定位示例

    SDKInitializer.initialize(getApplicationContext()); //当前视图 setContentView(R.layout.activity_map_api_demo); //创建地图对象 init(); final Button btn_location = (Button) findViewById(R...

    访微信语音聊天

    mAdapter = new RecorderAdapter(getApplicationContext(), mDatas); mListview.setAdapter(mAdapter); } private void initEvents() { mListview.setOnItemClickListener(this); } @Override public ...

    android 五子棋源码

    public class MainActivity extends Activity implements OnClickListener{ private ViewPager viewPager; private List<ImageView> imageViews; //滑动的图片集合 private String[] titles;//图片标题 ...

    高仿微信界面

    this.fragList = fragList; } @Override public CharSequence getPageTitle(int position) { return tabList.get(position); } @Override public Fragment getItem(int position) { return fragList....

    蓝牙APP通信

    Intent intent = new Intent(getApplicationContext(), ServiceActivity.class); intent.putExtra("device", scan_devices_dis.get(position) .GetDevice()); startActivity(intent); } ...

    android检查内存泄露

    refWatcher = LeakCanary.install(this); } //=============================================================================== public static RefWatcher getRefWatcher(Context context) { ...

    snackbar2-效果更丰富的snackbar.zip

    '使用Snackbar.with(getApplicationContext()) // context  .text("Single-line snackbar") // text to display  .show(this); // activity where it is displayed或// Dismisses the Snackbar...

    word源码java-Hello-Fan:高级软件实作,繁体字学习软件,你好繁!

    其他情况,确保activity销毁前,取消引用activity,用this就好了。 举个例子:公共SQLdm这个类,就使用getApplication(),因为不涉及UI。构建AlertDialog,就使用this来获取context。 关于SQLiteDatabase.close()...

    snackbar-master:在 Github 上提交 Snackbar

    with(getApplicationContext()) // context .text( " Single-line snackbar " ) // text to display .show( this ); // activity where it is displayed 但是,我建议您使用SnackbarManager来处理 Snackbars 队列:...

    SwipeListViewTest项目

    this.mSwipeListView = mSwipeListView ; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(final int position, View ...

    Android快速低耗自由更新--Zwr sdk

    Bmob initialize getApplicationContext appid 最后setZwrView "类名" this true false ; appid取自bmob后台 在“应用信息”里查看 “类名”为Expand端对应的类名 如“com test bmob FirstView” 编译...

    Android bindservice失败解决方法

    使用了TabActivity, TabActivity里的子Activity调用this.bindservice,导致失败。该问题属于Google Android的缺陷,由于TabActivity已经被弃用了,所以也一直没有修复这个问题。 解决方法: 使用...

    UdeskSDK-Android:UdeskSDK-Android

    mwebView = new WebView(getApplicationContext()); 改成 mwebView = new WebView(this); 修改原因解释: WebView传入Activity上下文可能会出现内存泄漏的隐患。 如果传入Application的上下文,使用下拉框字段...

    android基础教程之context使用详解

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

Global site tag (gtag.js) - Google Analytics