As android developer, using scope feature in activity and fragment.
--> So solution may be:
Sorry my bad English
try with Koin 2.0 Scope API
@arnaudgiuliani
I've read scope api of koin 2.0. But I can't see the way how to create and use dynamic add/remove scope definition. All scope must be pre-define as startKoin but not programmatic by code.
For ex:
Could you pls tell me how to do this in koin 2.0?
You have several ways to define a scope. A scope is a space where you will have a scoped definition.
You can create a scope with the scope function, to gather scoped definitions. Any scoped definition outside of this scope can be injected.
You can also base yourself on Android scopes (Activity, Fragment) to have scopes based on lifecycle components.
A scope instance is an instance of a scope.
In your case, use the getFragmentScope() in your fragment to inject your scoped definition.
module {
scope<MyFragment>{
scoped { MyPresenter() }
}
}
On each MyFragment instance, you will use val presenter MyPresenter : getFragmentScope().inject() or .get() to retrieve your MyPresenter instance for current scope.
Then you will have 1 instance of MyPresenter by instance of MyFragment.
Else use simply a factory to define your presenter. it will do the same in this simple case.
@arnaudgiuliani
thank for your support. I understands, I'll try as your answer.
Most helpful comment
@arnaudgiuliani
thank for your support. I understands, I'll try as your answer.