Parameters in by inject() are not lazy
See original GitHub issueAfter playing around a little bit with the new parameters i came accross another issue.
class MyFragment : Fragment() {
val presenter: MyPresenter by inject(parameters = mapOf("activity" to activity!!)
// or since support lib 27.1.0 we can call
val presenter: MyPresenter by inject(parameters = mapOf("activity" to requiredActivity())
}
if you instantiate this fragment the presenter gets created instantly and not on his first call. this leads to the following exception
Caused by: java.lang.IllegalStateException: Fragment MyFragment{3833b34} not attached to an activity.
currently a workaround is to inject the presenter like this
class MyFragment : Fragment() {
val presenter: MyPresenter by lazy { (StandAloneContext.koinContext as KoinContext).get<MyPresenter>("", mapOf("activity" to activity!!)) }
}
this works and the presenter gets only created after the first call
i think it would be great if by inject() has the same functionality as by lazy or if you will not change this direct instantiation you could provide another by injectLazy() function or so…
I am looking forward for your feedback
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Using Lazy Evaluation To Prevent Parameter Drilling in ...
While I do think injection does solve the problem of decoupling, it can face a huge flaw when you have tall class relation...
Read more >Dependency Injection using Koin - Medium
Dependency Inversion: Upper level modules should not depend on lower level ... by inject() - lazy evaluated instance from Koin container.
Read more >Lazy Injection Example — injectable 3.4.7 documentation
In our LazyInjection example class we inject the ServiceA lazily by specifying the parameter lazy=True to Autowired and we also inject the ...
Read more >Android Koin injected viewmodel with multiple same class ...
params.get() resolves the parameters by type. Since both are strings, it will match the first one in both cases. It works implicitly only...
Read more >Lazy Dependency Injection for .NET - DEV Community
The DoWork method uses either ServiceA or ServiceB depending on the value parameter. The important thing is that the method does not ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok. Fixed.
I migrate all params to be functional. Koin has now a very nice syntax to supply dynamic params (lambda directly opened for parameters):
Just migrate my MVP sample: https://github.com/Ekito/koin-samples/blob/0.9.1/samples/android-weatherapp/app/src/main/kotlin/org/koin/sampleapp/view/search/SearchActivity.kt#L23
You can play with Koin
0.9.1-alpha-5
👍The same with viewModel()… I try with @Bodo1981 solution and it works