据我所知,Android中的Application是一个单例(如果我错了请指正),我们始终只有一个应用程序上下文实例。
因此,从这个角度来看,在我的Application类中保存应用程序上下文是否是一种不好的做法?它会导致大量的内存泄漏吗?
这里是一个例子:
public class MyApp extends Application {
private static Context appContext = null; // <-- here is the thing!
@Override
public void onCreate() {
appContext = this;
}
public static Context getApplicationContextSingleton () {
return MyApp.appContext;
}
}
做这件事的原因是全局访问类(比如PreferencesManager),大多数都需要一个上下文的静态方法。所以,我考虑存储应用程序的上下文,而不是每次传递它(甚至是存储在实例中,可能会有问题)。但我没有看到哪些缺点?