代码之家  ›  专栏  ›  技术社区  ›  Halima

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

  •  0
  • Halima  · 技术社区  · 8 年前

    在我的 CallType 同学们,我把注入的数据 connectivityState.callBackState 为true,在我的 ConnectivitySte 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 CLASS
       if (connectivityState.callBackState) {
        }
    }    
    
    class ConnectivityState {
       var iaxState = false    
    }
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   Salomon BRYS    8 年前

    问题在于:

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

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

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

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