Hi all! I'm trying to wrap my head around how to b...
# koin
d
Hi all! I'm trying to wrap my head around how to bind and use things that are bound into the
activityRetainedScope()
. This is my setup:
Copy code
// MyActivity.kt
class MyActivity : AppCompatActivity(), AndroidScopeComponent {
    override val scope: Scope by activityRetainedScope()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            // I also tried CompositionLocalProvider(LocalKoinScope provides scope) { ... }
            KoinAndroidContext {
                AppNavHost()
            }
        }
    }
}

// AppNavHost.kt
fun AppNavHost(
    appBarActionsProvider: AppBarActionProvider = koinInject<AppBarActionProvider>(),
    ...
) { ... }

// MyModule.kt
fun myModule(): KoinModule =
    module {
        scope<ComponentActivity> {
            scopedOf(::AppBarActionHandler) binds arrayOf(AppBarActionProvider::class, AppBarActionObserver::class)
        }
        ...
    }
However, my injection of
AppBarActionProvider
is not found. I'm unsure what the
Scope
generic argument of
scope()
must be, the base class or something else?
Ok, got it working with providing
LocalKoinScope
with
scope
and using
scoped<MyActivity> { ... }
, now however I am not able to inject
AppBarActionObserver
in another injectee injected itself via
koinViewModel
. I guess the scope
koinViewModel
creates only knows of the root scope, but not my custom
activityRetainedScope
...
Ok, apparently one has to define those things that should benefit from things existing inside a specific scope in that scope as well, i.e. I had to move my
viewModelOf(::MyViewModel)
declaration inside my
scope<MyActivity> { }
block so that things provided inside that scope become accessible. Only downside I see here is that I always have to reference specific types here, i.e.
MyActivity
, to scope things, as
activityRetainedScope
does not allow me to specify another, more generic scope specifier, so things that should be scoped underneath Activities and that should also be shared between different activities do not work.
a
4.1 is bringing scope archetypes, allowing generic reuse of scopes - https://github.com/InsertKoinIO/koin/pull/2169