-
Notifications
You must be signed in to change notification settings - Fork 0
koin: Injecting the viewβmodel in activity
Devrath edited this page Mar 7, 2024
·
1 revision
class MainViewModel : ViewModel() {
// ViewModel logic
}
class MainActivity : ComponentActivity() {
val viewModel: MainViewModel by viewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
KoinDemoTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
}
}
}
}
}
val viewModelModules = module {
viewModelOf(::MainViewModel)
}
class KotlinApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@KotlinApplication)
modules(viewModelModules)
}
}
}