Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 669 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Koin how to inject outside of Android activity / appcompatactivity

#1
**[Koin][1]** 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:

class SplashScreenActivity : Activity() {

val sampleClass : SampleClass by inject()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}

with the `inject()` method.

But what about injecting stuff in places where Activity context is not available i.e. **outside of an Activity**?


[1]:

[To see links please register here]

Reply

#2
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?)

Reply

#3
Koin provides a solution for this using the `KoinComponent` interface. For example, if you need to get some dependencies in your repository then you can simply implement the KoinComponent interface. It gives you access to various Koin features such as `get()` and `inject()`. Use KoinComponent only when you can't rewrite the constructor to accept dependencies as constructor parameters.

```
class MyRepository: Repository(), KoinComponent {
private val myService by inject<MyService>()
}
```

**Constructor injection is better than this approach.**

For example, the same thing can be achieved by:

```
class MyRepository(private val service: MyService): Repository() {
...
}
```
And you can add the definition for instantiating this class in a koin module:

```
val serviceModule = module {
...

factory { MyService() }
}
val repositoryModule = module {
...

factory { MyRepository(get<MyService>()) }
}
```
Reply

#4
If you don't want to implement any interfaces then just take a look at how [`KoinComponent.inject()`][1] is implemented and do something similar yourself:

val foo by lazy { KoinPlatformTools.defaultContext().get().get<FooClass>() }

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through