InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
9.09k stars 719 forks source link

Support Scope parameter in Activity and Fragment delegate methods. #2040

Open mariusmorabosch opened 2 weeks ago

mariusmorabosch commented 2 weeks ago

The compose extensions allow you to inject a ViewModel while specifying a scope, but the activity and fragments one do not. They should both support the same parameters, otherwise one has to duplicate the source code to call resolveViewModel with a custom scope for fragments / activities.

Is there any reason why the fragment / activity do not support the scope parameter but the @Composable ones do?

arnaudgiuliani commented 1 day ago

on activity/fragment you don't pass the scope explicitly, as you resolve things from it. This ActivityScope setup the scope you will use in the background: https://github.com/InsertKoinIO/koin/blob/main/projects/android/koin-android/src/main/java/org/koin/androidx/scope/ScopeActivity.kt#L35

mariusmorabosch commented 1 day ago

@arnaudgiuliani What if you want to use a named scope though? Using the activity scope then everything is bound to the activity implicitly but if I want to have more than one scope on the same activity or just use a named scope to pass to child fragments and handle its lifecycle differently?