question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AndroidInjection / AndroidSupportInjection to inject custom class

See original GitHub issue

Is 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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:8

github_iconTop GitHub Comments

4reactions
ronshapirocommented, Jun 7, 2017

If it’s a class that Android constructs, you can inject the component into the fragment, and the fragment can call the injection:

@Subcomponent
interface FragSubcomponent extends AndroidInjector<Frag> {
  void inject(MyCustomView view);
  /* component builder */
}

class Frag extends DaggerFragment {
  @Inject FragSubcomponent component;

  void onCreateView() {
    component.inject(findViewById(R.id.customView));
  }
}

If the object is something that you can call new on, can you inject the type yourself?

3reactions
tbroyercommented, Jun 8, 2017

Or you could inject a MembersInjector<MyCustomView> rather than the full component.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Where to inject AndroidInjection in custom/util classes using ...
I am using dagger 2. And injecting my dependency in activities and fragments which is working fine. AndroidInjection.inject(this) for activities ...
Read more >
Using Dagger in Android apps
To inject an object in the activity, you'd use the appComponent defined in your Application class and call the inject() method, passing in...
Read more >
How to implement Dependency Injection in your app with ...
It will generate AndroidInjector for all Fragments defined in this class. Objects can be injected into Fragments using AndroidSupportInjection.
Read more >
New Android Injector with Dagger 2 — part 1 | by Mert SIMSEK
We don't add inject() and build() method to this component. MainActivityComponent has these methods from ancestor class. AndroidInjector ...
Read more >
Dagger by Tutorials, Chapter 16: Dagger & Android | Kodeco ...
class SplashActivity : AppCompatActivity() { // ... override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) // HERE super.onCreate( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found