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

时间:2017-10-23 12:46:23

标签: android dependency-injection kotlin kodein

在我的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    
}

感谢您的回复

1 个答案:

答案 0 :(得分:0)

问题在于:

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

您绑定到provider范围,这意味着每次要求实例时都会返回新实例

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

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