kodein - 注入的数据值不是数据的值检索

kodein - data's value injected are not data's value retrieve

在我的CallTypeclass中,我将注入的数据connectivityState.callBackState设置为true,在我的ConnectivitySteclass中初始化为false。但是在我的 IncomingCallBroadcastReceiver class 中,我的布尔数据 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 CLASS
   if (connectivityState.callBackState) {
    }
}    

class ConnectivityState {
   var iaxState = false    
}

问题出在这里:

bind<ConnectivityState>() with provider { ConnectivityState() }

您正在绑定到一个 provider 范围,这意味着 每次您请求实例时都会返回一个新实例

如果你想每次都返回同一个实例,你需要绑定一个singleton:

bind<ConnectivityState>() with singleton { ConnectivityState() }