AndroidInjection / AndroidSupportInjection to inject custom class
See original GitHub issueIs it possible to do that with the android utilities ? I have an: Application / Activity / Fragment component structure where my activity components are subcomponents of the application component, and the fragments components are subcomponents for the activities ones. Everything is working alright using:
AndroidInjection.inject(this)
for activities and AndroidSupportInjection.inject(this)
for fragments inside onCreate
and onAttach
respectively:
// for the activity
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
}
// for fragments
override fun onAttach(context: Context?) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
}
Injection works perfectly, but now I need to inject a different class using the fragment component. Is there any possible way to have access to the fragment / activity component from inside the activity / fragment and perform an injection over a different class? Let’s say it’s a customview with some injected dependencies, and I don’t want to create a different component for it (since that would be messy and I don’t really need to create a different scope for it, I should probably reuse the fragment / activity scope to inject it):
fragmentComponent.inject(myCustomView)
I would love to be able to do that in some way, but I see that AndroidInjection
and AndroidSupportInjection
are hiding too many details here and I the only possible choice for me to get this working would be to rollback the injection strategy to the “old style” explicit subcomponent builders inside the activity and the fragment.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:8
Top GitHub Comments
If it’s a class that Android constructs, you can inject the component into the fragment, and the fragment can call the injection:
If the object is something that you can call
new
on, can you inject the type yourself?Or you could inject a
MembersInjector<MyCustomView>
rather than the full component.