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.

Add custom module having constructor before AndroidInjection.inject()

See original GitHub issue

I’m testing about dagger.android. I wonder how to add custom module having constructor.

In usual, when we use AndroidInjection.inject(activity), It declares all module has no constructor, right?

example

@Module class ApiModule {
  @Provides fun retrofit(): Retrofit = Retrofit.Builder().build()
}

But in my case, I have some module having constructor

example

@Module class SomeModule(private val a : InterfaceA, private val b : InterfaceB) {
  @Provides fun a() : InterfaceA = a
  @Provides fun b() : InterfaceB = b
}

InterfaceA and InterfaceB need to be declared in Activity or Fragment

So now it has a big problem when doing injection.

class Presenter @Inject internal constructor(private val a : InterfaceA, private val b : InterfaceB) {
  // do something....
}

// before dagger.android
class AppActivity : Activity {
  @Inject lateinit var presenter : Presenter
  override fun onCreate(bundle: Bundle?) {
    super.onCreate(bundle)
    DaggerAppActivityComponent.builder()
        .SomeModule(SomeModule(a = xxx, b = yyy)
        .inject(this)
  }

}

// after dagger.android
class AppActivity : DaggerActivity {
  @Inject lateinit var presenter : Presenter
  override fun onCreate(bundle: Bundle?) {
    super.onCreate(bundle)
  }
}

My question is this : When we need module having custom constructor but its arguments cannot produce in dagger-graph. If we use AndroidInjection.inject(), then how do we add that module?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

17reactions
ronshapirocommented, Mar 5, 2017

There currently isn’t a way to do this. How do you know what values to pass to the module? Are they a function of the Activity, or the Activity’s Intent? If so, then you can use the activity binding do retrieve them in a provides method

2reactions
ronshapirocommented, Mar 5, 2017

Can you add a module like this to your subcomponent:

@Module
public abstract class TasksModule {
  @Provides
  static TasksContract.View provideTasksContractView(TasksActivity activity) {
    return (TasksFragment) activity.getFragmentManager().findFragmentById(R.id.contentFrame);
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Dagger 2 for dependency injection in Android - Tutorial
@Component : enable selected modules and used for performing dependency injection ... If you annotate a constructor with @Inject , Dagger 2 can...
Read more >
Using Dagger in Android apps
Use constructor injection with @Inject to add types to the Dagger graph whenever it's possible. When it's not: Use @Binds to tell Dagger...
Read more >
Using Dagger 2 to inject into service - android - Stack Overflow
I wrote the code from the top of my head, so there could be a typo or two. You do it just the...
Read more >
How to implement Dependency Injection in your app with ...
Instead of creating and configuring a new MyLibrary class object inside your class MyAppClass , you just pass or inject it into the...
Read more >
Dagger 2 – Rules of Engagement - Android Stuff
3. Components can be called in 2 ways. You will have seen calls to component. · 4. Use constructor injection · 5. Modules...
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