-
-
Notifications
You must be signed in to change notification settings - Fork 727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inject Activity #49
Comments
Hello, I'm currently writing recipes for such use. You can bind your Activity in your activity :
class Analytics() {
lateinit var activity: Activity
private val firebaseAnalytics: FirebaseAnalytics = FirebaseAnalytics.getInstance(activity)
fun setCurrentScreen(screenName: String) {
firebaseAnalytics.setCurrentScreen(activity, screenName, null)
}
} class MainActivity : AppCompatActivity() {
val presenter : MainPresenter by inject()
override fun onStart() {
super.onStart()
presenter.activity = this
}
} |
Thank you for your quick response. The issue is that I don't want my presenter to depend on any activity. The only class who should know about the activity is I would like to avoid doing something like this: class MainActivity : AppCompatActivity() {
val presenter : MainPresenter by inject()
override fun onStart() {
super.onStart()
presenter.analytics.activity = this
}
} |
Ok. Then, if you don't want to keep in in your presenter, pass it through function parameter? class MainPresenter(analytics: Analytics) {
fun doSomething(context : Context){
// ... use your activity here
}
} Activity and all Android components are somewhat "outside" of Koin. The only thing that we could do, is about Fragment instance (because they are created manually). |
Hmm I understand. So there is no nice way to provide an Activity to a Koin module. I don't know how Koin really works underneath yet, I'll have to study it. But it should be nice if we could develop a nice Koin solution to this. A lot of external frameworks depend on some Android component (like the Activity in the case of Firebase). Do you see it possible? |
Maybe a curren-activity-provider could me implemented using ActivityLifecycleCallbacks |
Interesting idea. Don't know if it's in the scope of Koin to provide such thing, or just a recipe to help about such situation. |
I think this has to be a "side" solution to implement if you want to keep an activity under the hand. This solution has to be documented for online reference. |
I came accross the same issue to inject android framework components (activity, fragment, ...) in some dependencies. so it would be great is koin can support this. if you do not want to include it in the core koin project how about adding it to the android module? in the meanwhile has someone a solution other than the already mentioned? btw GREAT LIBRARY!!! |
Similar issue trying to inject an instance of an Anko SQLite Database Helper. Would be amazing if KOIN could be Android Context-aware somehow to facilitate these kinds of dependencies. |
If I understand, could be nice to allow inject current Activity, right? given class: class Analytics(val activity: Activity) we could the a Koin module with val module = applicationContext{
factory { Analytics(currentActivity())}
} this way, each time you ask for |
That will be the most used case with activity. But maybe koin could provide a more generic way to inject also other classes val module = applicationContext {
factory { Analytics(currentContext<GenericType>()) }
} The provided (GenericType) class is the class where your Analytics class will be injected, e.g. class AnalyticsActivity : Activity, Navigator {
val analytics: Analytics by inject() // in this case GenericType can be (Analytics-)Activity or Navigator
} class AnalyticsFragment : Fragment, Navigator {
val analytics: Analytics by inject() // in this case GenericType can be (Analytics-)Fragment or Navigator
} Can this be achieved with koin? |
In Dagger, If want depend to Activity, At the timing of injecting Presenter, providing an instance of Activity to The javadoc of |
Great idea! class AnalyticsActivity : Activity, Navigator {
val analytics: Analytics by inject(this, this as Navigator, ...) // pass additional optional parameters to the inject delegate
} |
I 've add Koin parameters to release 0.9.0. you will be able to use parameters in your definition. Given class: class MyPresenter(val activity : MyActivity) We can use parameters to be injected with val module = applicationContext {
factory { params -> MyPresenter(params["activity"])}
} Injecting the parameter: class MyActivity : AppCompatActivity(){
// Ask for MyPresenter injection and provide parameters
val presenter : MyPresenter by inject( parameters = mapOf("activity" to this))
} stay tuned👍 |
Great to hear! Any timetable when 0.9.0 will be released? |
release |
"parameters" feature will be available for viewModels too ??? |
Yes ;) 👍
Currently merging some feature branches
On Tue, 27 Feb 2018 at 17:28 fredy-mederos ***@***.***> wrote:
"parameters" feature will be available for viewModels too ???
I noticed in v0.9.0-rc1 viewModels are being created with emptyMap of
parameters.
Thanks
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#49 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ACQFcPag2m-N73VWGbhYT8hVMMThucr_ks5tZC0NgaJpZM4SCHgP>
.
--
Arnaud GIULIANI - Mobile & Cloud
Tel : +33 6 78 81 72 12 - mail : [email protected]
EKITO - 15 rue Gabriel Péri 31000 Toulouse
web : www.ekito.fr / www.ekito.fr/people
|
Is this still the best way to achieve this? I made a little helper:
|
I am using LocationModule.class
MainActivity.class
|
Yes, injection parameters are the best in that case! |
Is there a way to create dependencies for scopes? I have similar usecase as above, but activity injects presenter, that depends on LocationProvider, that uses FusedLocationClient, requiring activity. So Activity should be part of the scope, not part of specific inject. |
^ agree, it would be nice if we could start a scope with a number of module params, in the same way we call startKoin() |
@dgngulcan thx |
How can the initial situation be solved? I have a similar situation: there is Interactor that needs activity reference and I'm injecting this interactor into viewModel constructor, but this way I can't use custom arguments.
|
@SYtor you could do something like:
and in your activity:
|
@bkoruznjak |
@makaroffandrey makes a good point. @arnaudgiuliani any suggestion to avoid leaks? |
@moizalidv It's a shame that Koin cannot do something like that out of the box. |
You can do this with My example is for MainNavigation:
my
then I can easily use my dependency (for example, MainNavigation):
|
Hi,
Koin is awesome but I'm having an issue. Some libraries, like Firebase Analytics, depend on the current Activity to perform some actions like setting a current screen.
Is this possible with Koin? If yes, then I don't see how.
The text was updated successfully, but these errors were encountered: