0

在我的CallType课堂上,我将注入的数据connectivityState.callBackState设置为 true,在ConnectivitySte课堂上将其初始化为 false。但是在我的IncomingCallBroadcastReceiver课堂上,我的布尔数据callBackState不是truebut 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    
}
4

1 回答 1

0

问题出在这里:

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

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

如果您希望每次都返回相同的实例,则需要绑定到 a singleton

bind<ConnectivityState>() with singleton { ConnectivityState() }
于 2017-10-23T14:10:23.637 回答