在我的CallType
课程中,我将我的注入数据connectivityState.callBackState
设为true,在我的ConnectivitySte
课程中将其初始化为false。但在我的IncomingCallBroadcastReceiver
课程中,我的学习数据callBackState
不是true
,而是false
。
class CallType {
val kodein = Kodein {
bind<ConnectivityState>() with provider { ConnectivityState() }
}
private val connectivityState: ConnectivityState = kodein.instance()
fun call(number: String) {
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\
connectivityState.callBackState = true
}
class IncomingCallBroadcastReceiver : KodeinBroadcastReceiver() {
private val connectivityState: ConnectivityState by instance()
override fun onBroadcastReceived(context: Context, intent: Intent) {
//!!!!!!!!!!!!!! IT'S FALSE HERE WHEREAS I PUT IT TO TRUE IN CALLTYPE CALSS
if (connectivityState.callBackState) {
}
}
class ConnectivityState {
var iaxState = false
}
感谢您的回复
答案 0 :(得分:0)
问题在于:
bind<ConnectivityState>() with provider { ConnectivityState() }
您绑定到provider
范围,这意味着每次要求实例时都会返回新实例。
如果您希望每次都返回相同的实例,则需要绑定到singleton
:
bind<ConnectivityState>() with singleton { ConnectivityState() }