作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在调查 Dagger2
的使用情况在我的 Android 应用程序中。
implementation 'com.google.dagger:dagger:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
@Inject
ViewModel 及其关联的存储库
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
@Singleton
public class DaggerViewModelFactory implements ViewModelProvider.Factory {
private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators;
@Inject
public DaggerViewModelFactory(final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
this.creators = creators;
}
@SuppressWarnings("unchecked")
@Override
public <T extends ViewModel> T create(final Class<T> modelClass) {
Provider<? extends ViewModel> creator = creators.get(modelClass);
if (creator == null) {
for (Map.Entry<Class<? extends ViewModel>, Provider<ViewModel>> entry : creators.entrySet()) {
if (modelClass.isAssignableFrom(entry.getKey())) {
creator = entry.getValue();
break;
}
}
}
if (creator == null) {
throw new IllegalArgumentException("unknown model class " + modelClass);
}
try {
return (T) creator.get();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import dagger.Binds;
import dagger.Module;
import dagger.multibindings.IntoMap;
@Module
public abstract class ViewModelModule {
@Binds
abstract ViewModelProvider.Factory bindViewModelFactory(final DaggerViewModelFactory factory);
@Binds
@IntoMap
@ViewModelKey(StilettoViewModel.class)
abstract ViewModel provideStilettoViewModel(final StilettoViewModel stilettoViewModel);
}
androidx.lifecycle.AndroidViewModel
.
import android.app.Application;
import java.util.Map;
import javax.inject.Provider;
import javax.inject.Singleton;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
@Singleton
public class DaggerAndroidViewModelFactory extends ViewModelProvider.AndroidViewModelFactory {
private final Application application;
private Map<Class<? extends AndroidViewModel>, Provider<AndroidViewModel>> creators;
/**
* Creates a {@code AndroidViewModelFactory}
*
* @param application an application to pass in {@link AndroidViewModel}
*/
public DaggerAndroidViewModelFactory(@NonNull final Application application) {
super(application);
this.application = application;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull final Class<T> modelClass) {
return super.create(modelClass);
}
}
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.ViewModelProvider;
import dagger.Binds;
import dagger.Module;
import dagger.multibindings.IntoMap;
@Module
public abstract class ViewModelModule {
@Binds
abstract ViewModelProvider.AndroidViewModelFactory bindAndroidViewModelFactory(final DaggerAndroidViewModelFactory factory);
@Binds
@IntoMap
@ViewModelKey(MyAndroidViewModel.class)
abstract AndroidViewModel provideArticlesViewModel(final ArticlesViewModel articlesViewModel);
}
error: [Dagger/MissingBinding] MyAndroidViewModel cannot be provided without an @Inject constructor or an @Provides-annotated method.
androidx.lifecycle.AndroidViewModel
与 Dagger 2?
最佳答案
如果您有 @Module
那@Provides
Application
(或者你使用 @BindsInstance
+ @Component.Builder
,老实说两者都有效):
@Module
public class AppModule {
private final Application app;
public AppModule(Application app) {
this.app = app;
}
@Provides
Application app() { return app; }
}
@Component(modules={AppModule.class, ...})
@Singleton
public interface AppComponent {
}
public class MyViewModel extends AndroidViewModel {
public MyViewModel(Application app) {
...
}
}
@Inject
一起使用构造函数:
public class MyViewModel extends ViewModel {
@Inject
MyViewModel(Application app) {
...
}
}
DaggerViewModelFactory
像任何其他常规 ViewModel 类一样使用它。 .
关于android - 如何用 Dagger2 @Inject AndroidViewModel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55142614/
我是一名优秀的程序员,十分优秀!