根据 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
考虑到内存泄漏、速度、一般性能,在所有这些可能性之间,什么是更好的方法?
最佳答案
你应该看看 this question - 基本上和你的一样。
还有开发者文档 Avoiding memory leaks为您提供合理使用各种方法的一些情况的合理解释。
关于android - 获取上下文的更好方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8501762/