写道
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 就正确。
分享到:
相关推荐
Intent intent=new Intent(Main2Activity.this,CCwantPhotoBrowserActivity.class); intent.putExtra("CCwantPhotoList",(Serializable) mData); intent.putExtra("CCwantPhotoPosition",(Serializable)position)...
Button button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new ButtonClickListener()); } private final class ButtonClickListener implements View.OnClickListener{ ...
大家好,今天给大家分享一下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....
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 ...
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....
Intent intent = new Intent(getApplicationContext(), ServiceActivity.class); intent.putExtra("device", scan_devices_dis.get(position) .GetDevice()); startActivity(intent); } ...
refWatcher = LeakCanary.install(this); } //=============================================================================== public static RefWatcher getRefWatcher(Context context) { ...
'使用Snackbar.with(getApplicationContext()) // context .text("Single-line snackbar") // text to display .show(this); // activity where it is displayed或// Dismisses the Snackbar...
其他情况,确保activity销毁前,取消引用activity,用this就好了。 举个例子:公共SQLdm这个类,就使用getApplication(),因为不涉及UI。构建AlertDialog,就使用this来获取context。 关于SQLiteDatabase.close()...
with(getApplicationContext()) // context .text( " Single-line snackbar " ) // text to display .show( this ); // activity where it is displayed 但是,我建议您使用SnackbarManager来处理 Snackbars 队列:...
this.mSwipeListView = mSwipeListView ; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(final int position, View ...
Bmob initialize getApplicationContext appid 最后setZwrView "类名" this true false ; appid取自bmob后台 在“应用信息”里查看 “类名”为Expand端对应的类名 如“com test bmob FirstView” 编译...
使用了TabActivity, TabActivity里的子Activity调用this.bindservice,导致失败。该问题属于Google Android的缺陷,由于TabActivity已经被弃用了,所以也一直没有修复这个问题。 解决方法: 使用...
mwebView = new WebView(getApplicationContext()); 改成 mwebView = new WebView(this); 修改原因解释: WebView传入Activity上下文可能会出现内存泄漏的隐患。 如果传入Application的上下文,使用下拉框字段...
代码如下:public class MyActivity extends Activity { public void method() { mContext = this; // since Activity extends Context mContext = getApplicationContext(); mContext = get