我对Koin并不陌生,我正在创建一个object实例,并为其赋予一个范围为ID SESSION的范围,因此在关闭它之后。我希望在下一次访问它时为其创建一个新实例。
val InkModule = module {
scope("SESSION") { providesInkDevice(get(), get()) }
}
fun providesInkDevice(application: Application, preferences: PreferenceUtility): InkDevice {
return InkDeviceFactory.createClient(application, Gson().fromJson(preferences.deviceInfo, InkDeviceInfo::class.java)) }
所以现在当我注入一个实例时,我总是创建一个作用域
private val inkDevice: InkDevice by inject()//Lazy injection
try {
scope = getKoin().getScope("SESSION")
} catch (e: NoScopeFoundException) {
scope = getKoin().createScope("SESSION")
Timber.e(e, "Cannot create new scope")
}
inkDevice.connect()//here a new instance is created when we access it first time
现在一段时间后,我想重新创建我的Object(inkDevice)的新实例 , 所以我决定做scope.close
scope = getKoin().getScope("SESSION")
inkDevice.dispose()
preferenceUtility.deviceInfo = ""
scope = getKoin().createScope("SESSION")
但是在我关闭会话后,下次我访问它时将无法创建新实例。