0

i'm new on kotlin and kodein development. I want to inject data to a simple class which extends nothing.

I have my MainActivity which extends KodeinAppCompatActivity(), my fragment which extends KodeinSupportFragment() calls a function from my simple class CallType. But this function must to change a boolean from an other simple class ConnectivitySate. I don't want to use static value.

Below, my code :

    class App : Application(), KodeinAware {

override val kodein by Kodein.lazy {
    import(autoAndroidModule(this@App))

    bind<CallType>() with instance(CallType())
    bind<ConnectivityState>() with instance(ConnectivityState())
    bind<ContactData>() with instance(ContactData())

}

override fun onCreate() {
    super.onCreate()
    registerActivityLifecycleCallbacks(androidActivityScope.lifecycleManager)
}        

MainActivity :

    class MainActivity : KodeinAppCompatActivity() {        

My Fragment :

class JournalFragment : KodeinSupportFragment(){

  private val callType: CallType by instance()

  @SuppressLint("MissingSuperCall")
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    initializeInjector()
}

  override fun onCreateView(inflater: LayoutInflater?, container: 
          ViewGroup?,savedInstanceState: Bundle?): View? {

      // !! CALL MY FUNCTION !!
      callType.call(callType.callNumber)

  }

 ....

 @SuppressLint("MissingSuperCall")
 override fun onDestroy() {
    super.onDestroy()
    destroyInjector()
}

My simple class :

class CallType {

fun call(number: String) {

   // !! I want to change gsmState value from ConnectivityState class
   connectivityState.gsmState = true

}

My ConnectivityState class :

class ConnectivityState {

    var gsmState = false

}

It is an example among many others, because in lots of situations, i'm blocked like that. I have try lots of things but i always have like error : value not injected

Thank you very much for your reply..

Halima
  • 5
  • 1

1 Answers1

0

When you call super.onCreate(), it calls onCreateView, so the line callType.call(callType.callNumber) is called before initializeInjector().

Note that you should always call initializeInjector() before calling super.onCreate():

override fun onCreate(savedInstanceState: Bundle?) {
    initializeInjector()
    super.onCreate(savedInstanceState)
}
Salomon BRYS
  • 9,247
  • 5
  • 29
  • 44
  • Thak you but it's weird because it works when i call `callType.call (callType.callNumber)`, but i'll change it. My problem is in CallType class. I want to inject value in this class. But i don't know how because it extends nothing. – Halima Oct 17 '17 at 07:34
  • i'm really blocked... if you have any idea. Thak you for your help – Halima Oct 17 '17 at 10:46
  • You can pass it the `kodein` property at constructor, and then use standard retrieval methods : https://salomonbrys.github.io/Kodein/#_dependency_retrieval – Salomon BRYS Oct 17 '17 at 12:28
  • Thank you, but I find it hard to understand in your doc. Can you show me an example with my `CallType` and `ConnectivitySate` above classes, for example ? – Halima Oct 18 '17 at 09:23