Created
May 3, 2019 06:34
-
-
Save seyedjafariy/cae83cb350e6ddc02d4e80db8a972c7a to your computer and use it in GitHub Desktop.
A simple dagger setup in a single module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Application | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import dagger.Component | |
import dagger.Module | |
import javax.inject.Singleton | |
@Module | |
object AppModule{ | |
} | |
@Singleton | |
@Component(modules= [AppModule::class]) | |
interface AppComponent{ | |
} | |
@Module | |
object FeatureModule{ | |
} | |
@Component(dependencies = [AppComponent::class], modules = [FeatureModule::class]) | |
interface FeatureComponent{ | |
fun inject(activity : FeatureActivity) | |
} | |
class App : Application(){ | |
lateinit var component : AppComponent | |
override fun onCreate() { | |
super.onCreate() | |
component = DaggerAppComponent | |
.builder() | |
.build() | |
} | |
} | |
class FeatureActivity : AppCompatActivity(){ | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val appComponent = (applicationContext as App).component | |
DaggerFeatureComponent.Builder() | |
.appComponent(appComponent) | |
.build() | |
.inject(this) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment