Skip to content

Instantly share code, notes, and snippets.

@enginebai
Created January 5, 2024 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enginebai/706a7fc9e4264323bdaee31401f76934 to your computer and use it in GitHub Desktop.
Save enginebai/706a7fc9e4264323bdaee31401f76934 to your computer and use it in GitHub Desktop.
@Singleton
@Component(
modules = [
AppModule::class
]
)
interface AppComponent {
fun inject(activity: MainActivity)
}
@Module
class AppModule {
@Provides
fun provideUser(): User {
return User.getInstance()
}
}
class MyApplication : Application {
lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
DaggerAppComponent.builder()
.build()
}
}
class MainActivity : Activity {
@Inject
lateinit var initUser: User
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(application as MyApplication).appComponent.inject(this)
// You can manipulate initUser here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment