Koin

作者: 闫鹏飞写字的地方 | 来源:发表于2021-09-08 13:42 被阅读0次

    引包

        implementation "org.koin:koin-android:2.2.0-rc-3"
        implementation "org.koin:koin-androidx-viewmodel:2.2.0-rc-3"
    

    Koin 怎么使用?

    对象

    data class Gril (val age:Int, val name:String)
    

    Application

    import android.app.Application
    import org.koin.android.ext.koin.androidContext
    import org.koin.android.ext.koin.androidFileProperties
    import org.koin.android.ext.koin.androidLogger
    import org.koin.core.context.GlobalContext.startKoin
    import org.koin.core.logger.Level
    
    class App : Application(){
    
        override fun onCreate() {
            super.onCreate()
            startKoin {
                androidLogger(Level.DEBUG)
                androidContext(this@App)
                androidFileProperties()
                modules(listOf(girlModule))
            }
        }
    
    }
    

    MainActivity

    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.util.Log
    import org.koin.android.ext.android.inject
    
    class MainActivity : AppCompatActivity() {
    
        val TAG:String = "MainActivityTAG"
    
        private val girl by inject<Gril>()
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            Log.i(TAG, "girl memory address:"+ girl.toString() +",girl age:"+girl.age+",girl name:"+girl.name)
        }
    }
    

    相关文章

      网友评论

          本文标题:Koin

          本文链接:https://www.haomeiwen.com/subject/knzhwltx.html