Reputation: 22696
Android Studio 3.4
dagger-android 2.21
How to use pass the activity
to an dagger module using the new dagger-android
Before using the older version of dagger we could pass the Activity in the constructor and return that in the provider method. But I not sure how to do that with dagger-android
I have the following module. However, dagger doesn't now about the ForecastActivity
.
@Module
class ActivityModule {
@Reusable
@Provides
fun provideRetryListener(forecastActivity: ForecastActivity): RetryListener {
return forecastActivity
}
}
The RetryListener
is a interface that the ForecastActivity
implements. I want to be able to inject this RetryListener
into my RetryFragment
i.e.
class RetryFragment : Fragment() {
@Inject
lateinit var retryListener: RetryListener // Inject here
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
AndroidSupportInjection.inject(this)
super.onCreateView(inflater, container, savedInstanceState)
retryListener.onRetry() // usage like this
return inflater.inflate(R.layout.failurecase_layout, container, false)
}
}
In the forecastActivity
class ForecastActivity : AppCompatActivity(), RetryListener {
@Inject
lateinit var forecastPresenter: ForecastPresenter
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
}
override fun onRetry() {
/* do something here */
}
}
My ActivityBuilder is the following:
@Module
abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = [ActivityModule::class])
abstract fun injectIntoRetryFragment(): RetryFragment
}
My component is this:
@Singleton
@Component(modules = [
AndroidSupportInjectionModule::class,
ActivityBuilder::class,
ActivityModule::class])
interface StockComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: StockApplication): Builder
fun build(): StockComponent
}
fun inject(application: StockApplication)
}
And my Application is:
class StockApplication : Application(), HasActivityInjector, HasSupportFragmentInjector {
@Inject
lateinit var dispatchingAndroidActivityInjector: DispatchingAndroidInjector<Activity>
@Inject
lateinit var dispatchingAndroidFragmentInjector: DispatchingAndroidInjector<Fragment>
override fun onCreate() {
super.onCreate()
DaggerStockComponent
.builder()
.application(this)
.build()
.inject(this)
}
override fun activityInjector(): AndroidInjector<Activity> {
return dispatchingAndroidActivityInjector
}
override fun supportFragmentInjector(): AndroidInjector<Fragment> {
return dispatchingAndroidFragmentInjector
}
}
So the question is when using dagger-android how can I inject the RetryListener
into the RetryFragment
that the RetryListener
is implemented by the ForecastActivity
?
Many thanks in advance
Upvotes: 0
Views: 795
Reputation: 8231
I can't see a module that provides your ForecastActivity
, i.e.:
@ContributesAndroidInjector
abstract fun forecastActivity(): ForecastActivity
The Activity
will need to implement HasSupportFragmentInjector
, and supply a DispatchingAndroidInjector<Fragment>
Upvotes: 1