salomonbrys
06/28/2018, 8:22 AMval MyActivity : Activity(), KodeinAware {
val db: Database by instance()
val manager = MyManager(db)
}
The db
value is lazy: it will get fetched only when needed.
However, the manager
value is not: it will get created at class initialization.
Because the Kodein instance is located in the Application
class, the Activity needs to be properly attached to the application, which is why, in Android, you cannot access to dependencies before onCreate.
In this example, myManager
is created at initialization, so it asks for the db
value. Because it is its first access, the db
value is fetched from Kodein. However, because it is first Kodein
access, the Kodein instance itself has to be retrieved from the applicationContext
. But, at initialization, the activity is not properly attached ! Hence the exception.