Questions tagged [koin]

Koin is a Dependency Injection framework for Kotlin developers.

Koin is a pragmatic lightweight open-source dependency injection framework for Kotlin developers.

Links:

482 questions
10
votes
4 answers

Inject viewModel to @Composable

I have viewModel for my ProfileScreen. @Composable fun ProfileScreen() { val viewModel: ProfileViewModel = viewModel() ... } Every time when I call ProfileScreen, new viewModel is created. How can I created only one viewModel instance for…
10
votes
1 answer

How to get Context in unit test to create Room database in memory database object

I'm trying to test this one function in my application repository class which performs a database insertion. I'm using Koin as my dependency injection library. To do the testing I need to create a Database version that gets built in memory. To…
9
votes
2 answers

Jetpack Compose Preview not working when using Koin for Dependency Injection

I want to use Jetpack Compose in my App. I am already using Koin for DI. Because I have a lot of convenience methods in my BaseFragment I want to inherit from it and build the corresponding view with compose. Now the Problem is that when using DI in…
9
votes
1 answer

How to manage User Scope using Koin?

I'm trying to create a user scope using Koin. When the user is logged, I'm creating the scope : val scope = getKoin().createScope("USER_SCOPE") And when the user clicks on logout, I'm destroying the scope scope?.let {userScope -> …
Mathieu H.
  • 800
  • 8
  • 21
8
votes
1 answer

How to initialize Koin viewmodel with parameters inside a composable?

My viewmodel module looks like this: val viewModelModule = module { viewModel { (id: Int, user: String, email: String) -> MyViewModel(get(), get(), id = id, user = user, email = email) } } so the viewmodel accepts a total of five…
crossdeath
  • 95
  • 1
  • 7
8
votes
1 answer

What is the proper way to provide Context to a DataSource with Clean Architecture, MVVM and Koin?

I am developing an Android application with Kotlin in which I need to get the current location of the mobile device. I've already found a way to do it in various examples, but I don't know how to integrate this logic according to Clean Architecture…
JoeM
  • 81
  • 2
8
votes
1 answer

Koin sharedViewModel with SavedStateHandle

I have single activity application and number of fragments. Some of these fragments are using my viewmodel, typically like this: private val myViewModel: MyViewModel by sharedViewModel() What if I want to have the model both shared and keep its…
ror
  • 3,295
  • 1
  • 19
  • 27
8
votes
2 answers

Trying to use koin but doesnt work correctly on android

I'm trying to implement Koin in my project. So far, I did this: My shared preferences class: class MPCUtilSharedPreference(private val sharedPreferences: SharedPreferences{} I want to inject that class in other classes. So, I create my…
LordCommanDev
  • 922
  • 12
  • 38
8
votes
4 answers

Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Factory,primary_type

I am developing news app I have implemented koin with viewmodel in fragment class but I am getting following error when I test code on emulator java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at…
Edgar
  • 860
  • 1
  • 17
  • 38
8
votes
1 answer

How to inject a ViewModel with Koin in Kotlin?

How do we inject ViewModel with dependency using Koin? So For Example I have a ViewModel thats like this: class SomeViewModel(val someDependency: SomeDependency, val anotherDependency: AnotherDependency): ViewModel() Now the official docs here,…
Archie G. Quiñones
  • 11,638
  • 18
  • 65
  • 107
7
votes
1 answer

Koin share dependencies scoped to nested graph

I'm wondering how to properly scope dependencies with Koin library. Since Google recommends a single Activity architecture the AndroidX Navigation lib has become a key library to facilitate this by easily swapping Fragments. A typical modern Android…
7
votes
2 answers

Koin 2.2.1 : I cannot use "by viewModel" in Activity

Cannot use "by viewModel" from Activity I want to inject a ViewModel for an Activity, so I tried this. But it was failed, Android Studio cannot find reference of it. private val mainViewModel: MainViewModel by viewModel() My activity extends…
Stanley Ko
  • 3,383
  • 3
  • 34
  • 60
7
votes
3 answers

startKoin in KoinTest-class throws "A KoinContext is already started"

I'm using "withTestAppliction" in one of my tests to test if the route works. Before all Tests the DB-Table "cats" should have no entries. To get the DAO I need Koin in this Test but if conflicts with "withTestAppliction" where Koin will also be…
Mike Mitterer
  • 6,810
  • 4
  • 41
  • 62
7
votes
3 answers

Koin No definition found for class, but already declared

Im trying to implement a Permission Injection using Koin as my D.I however when i execute my ViewModel that requires the PermissionRepository i keep receiving the error. No definition found for…
Luis Cardoza Bird
  • 1,265
  • 4
  • 24
  • 43
7
votes
1 answer

Unit Testing with mocking of FusedLocationProviderClient and dependency injection with Koin

Within our Android app we are using Google Maps API in order to show the users location. At the moment we are using Koin in order to provide a parametered injection (it requires an activity) of the FusedLocationProviderClient dependency into our…
1 2
3
32 33