Skip to content

Bind to the Application

Valery edited this page Jan 8, 2019 · 7 revisions

First thing first, add the lifecycle callbacks listeners. In this step, the library will register the lifecycle callbacks listener so every component will be destroyed after the destruction of the owner.

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        XInjectionManager.init(this)
    }
}

If your application has a component, for example, an AppComponent that contains all the app dependencies, implement the IHasComponent<AppComponent> interface in your Application class.

The method getComponent must return the AppComponent.

Also, you can override the method getComponentKey, but it doesn't make any sense in this case.

class App : Application(), IHasComponent<AppComponent> {
    override fun onCreate() {
        super.onCreate()
        XInjectionManager.init(this)
        XInjectionManager.bindComponent<AppComponent>(this).inject(this)
    }

    override fun getComponent(): AppComponent = DaggerAppComponent.create()
}

Then you need to call the method bindComponent<AppComponent>(this) to get the component. The method checks whether the component exists in the local store or not.

  • If the component doesn't exist in the local store, the library gets a component by calling the getComponent method of the owner and binds the component with the key that is provided by the getComponentKey method and finally returns the component.
  • But if the component exists, the library just returns it.

If you are using AndroidX, you must use the XInjectionManager from

implementation "com.github.valeryponomarenko.componentsmanager:androidx:LATEST_VERSION"

If you are using AppCompat (support library), you must use the CompatInjectionManager from

implementation "com.github.valeryponomarenko.componentsmanager:appcompat:LATEST_VERSION"
Clone this wiki locally