在使用Dagger2进行依赖注入时,你可以通过在ViewModel的构造函数中注入WorkManager实例来实现依赖注入。以下是一个详细的步骤指南,帮助你完成这一任务。
首先,确保在你的build.gradle
文件中添加了Dagger2和WorkManager的依赖。
dependencies {
implementation "com.google.dagger:dagger:2.x"
kapt "com.google.dagger:dagger-compiler:2.x"
implementation "androidx.work:work-runtime-ktx:2.x.x"
}
创建一个ViewModel,并在其构造函数中注入WorkManager实例。
import androidx.lifecycle.ViewModel
import androidx.work.WorkManager
import javax.inject.Inject
class MyViewModel @Inject constructor(
private val workManager: WorkManager
) : ViewModel() {
// ViewModel implementation
}
为了让Dagger2能够提供ViewModel实例,你需要创建一个ViewModelFactory。
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import javax.inject.Inject
import javax.inject.Provider
import javax.inject.Singleton
@Singleton
class ViewModelFactory @Inject constructor(
private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
val creator = creators[modelClass] ?: creators.entries.firstOrNull {
modelClass.isAssignableFrom(it.key)
}?.value ?: throw IllegalArgumentException("Unknown model class $modelClass")
return try {
@Suppress("UNCHECKED_CAST")
creator.get() as T
} catch (e: Exception) {
throw RuntimeException(e)
}
}
}
创建一个Dagger Module,用于提供WorkManager和ViewModel的依赖。
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.work.WorkManager
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.multibindings.IntoMap
import dagger.multibindings.StringKey
@Module
abstract class ViewModelModule {
@Binds
abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
@Binds
@IntoMap
@StringKey("MyViewModel")
abstract fun bindMyViewModel(viewModel: MyViewModel): ViewModel
companion object {
@Provides
fun provideWorkManager(): WorkManager {
return WorkManager.getInstance()
}
}
}
创建一个Dagger Component,将所有模块连接起来。
import dagger.Component
import javax.inject.Singleton
@Singleton
@Component(modules = [ViewModelModule::class])
interface AppComponent {
fun inject(activity: MainActivity)
}
在你的Activity或Fragment中使用Dagger注入ViewModel。
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import javax.inject.Inject
class MainActivity : AppCompatActivity() {
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
private val myViewModel: MyViewModel by viewModels { viewModelFactory }
override fun onCreate(savedInstanceState: Bundle?) {
(application as MyApplication).appComponent.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Use myViewModel
}
}
最后,创建一个自定义的Application类,并初始化Dagger。
import android.app.Application
class MyApplication : Application() {
lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.create()
}
}
领取专属 10元无门槛券
手把手带您无忧上云