获取Context的更好方法是什么?

gru*_*unk 11 android android-context

根据这个答案或android的文档,有几种方法可以在应用程序中获取Context并将其传递给其他类/方法/ anyuneed.

假设我在Foo活动中并且需要将上下文传递给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 
Run Code Online (Sandbox Code Playgroud)

考虑到内存泄漏,速度,一般性能,所有这些可能性之间的更好方法是什么?

kas*_*rch 2

你应该看看这个问题- 它基本上与你的问题相同。

此外,有关避免内存泄漏的开发人员文档还为您提供了对某些情况的合理解释,在这些情况下可以合理使用各种方法。