【发布时间】:2011-12-14 08:59:52
【问题描述】:
根据this answer 或android 的文档,有几种方法可以在应用中获取上下文并将其传递给其他类/方法/whateveruneed。
假设我在 Foo Activity 中并且需要将上下文传递给 Bar 的构造函数。
Bar bar = new Bar(Foo.this);
Bar bar2 = new Bar(this); //same as first i guess
Bar bar3 = new Bar(getApplicationContext());
Bar bar4 = new Bar(getBaseContext());
Bar bar5 = new Bar(MyApp.getContext); // get context statically
考虑到内存泄漏、速度、一般性能,在所有这些可能性之间有什么更好的方法?
【问题讨论】: