Created
January 5, 2024 08:42
-
-
Save enginebai/706a7fc9e4264323bdaee31401f76934 to your computer and use it in GitHub Desktop.
This file contains 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
@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