【发布时间】:2020-07-30 04:34:41
【问题描述】:
我有一个Authenticator 类,它需要一个context 来使用SharedPreferences 完成一些任务。我在我的自定义应用程序类中创建了一个类的单例,这样我就不需要每次都传递上下文来创建我的Authenticator 的实例。这是正确的方法还是会导致内存泄漏或其他问题?
我的Application 班级:
class App : Application() {
override fun onCreate() {
super.onCreate()
//Plant Timber for Logging in Debug Build
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
Authenticator.createInstance(this)
}
...
....
}
在其他类上,我可以像这样访问单例:
Authenticator.INSTANCE
【问题讨论】:
标签: android kotlin singleton android-context