Koin how to inject outside of Android activity / appcompatactivity

前端 未结 3 864
既然无缘
既然无缘 2020-12-16 10:13

Koin is a new, lightweight library for DI and can be used in Android as well as in standalone kotlin apps.

Usually you inject dependencies like this

3条回答
  •  感动是毒
    2020-12-16 10:40

    There is the KoinComponent which comes to the rescue. In any class you can simply:

    class SampleClass : KoinComponent {
    
        val a : A? by inject()
        val b : B? by inject()
    }
    

    Extending KoinComponent gives you access to inject() method.

    Remember that usually it's enough to inject stuff the usual way:

    class SampleClass(val a : A?, val b: B?)
    

提交回复
热议问题