草庐IT

dagger-2

全部标签

android - 从 ViewModel 访问 strings.xml

我正在使用Dagger2DataBindng和新的AndroidLifecycle组件,它们具有ViewModels。在我的ViewModel中,我如何才能访问我的strings.xml?起初我在想,将Context注入(inject)到viewModel中,但是,这只会泄漏内存。还有其他方法吗? 最佳答案 有一个AndroidViewModel,它接收Application实例作为参数。来自文档:ApplicationcontextawareViewModel.Subclassesmusthaveaconstructorwhich

android - Dagger -Android : Missing setters for modules

数据模块:@ModuleclassDataModuleconstructor(application:App){privatevardb:Database=Room.databaseBuilder(application.applicationContext,Database::class.java,"database.db").build()@Provides@PerApplicationfunprovideDatabase():Database{returndb}应用:classApp:DaggerApplication(){@InjectlateinitvaractivityDi

android - Dagger:组件中存在具有匹配键的绑定(bind)

我正在使用Dagger2.16并关注this我的Dagger实现的文章。在我只有一个Activity(HomeActivity)之前,此实现一切正常。我一开始在SplashScreenActivity中实现Dagger。我开始收到此错误。这是我项目中的一些代码AppComponent.kt@Singleton@Component(modules=[AndroidInjectionModule::class,AppModule::class,ActivityBuilder::class,ServiceBuilder::class,BroadcastRecieverBuilder::cla

java - Android Architecture Blueprints "todo‑mvp‑dagger",@Provides for TasksRepository 在哪里?

从此处查看Android架构蓝图“todo‑mvp‑dagger”:https://github.com/googlesamples/android-architecture.我试图了解Dagger2的实现,但我似乎无法弄清楚他们如何让Dagger2提供TasksRepository。他们有@Provides用于“provideTasksLocalDataSource”和“provideTasksRemoteDataSource”,但实际的TaskRepository在哪里?在TodoApplication类中,他们有这个:@InjectTasksRepositorytasksRepo

android - Dagger2,同时提供不同API的Retrofit实例

在我的项目中,我使用Retrofit并尝试使用Dagger来注入(inject)依赖项。我还有2个具有不同API的Retrofit服务。我需要同时使用2个具有不同baseUrls的不同API。我卡在这里,不知道下一步该做什么。我的应用模块:@ModulepublicclassApplicationModule{privateStringFIRST_API_URL="https://first-api.com";privateStringSECOND_API_URL="https://second-api.com";privateStringmBaseUrl;privateContext

java - Dagger 2 : error while getting a multiple instances of same object with @Named

我怎样才能得到像游标一样返回类型相同的多个实例例如:-Module@CursorScopepublicclassCursorModule{@ProvidesCursorprovideSongCursor(@Named("Song")Musicianmusician){returnmusician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,newString[]{BaseColumns._ID,MediaStore.Audio.AudioCo

android - 在构造函数中仅注入(inject)某些参数

我有主持人classPhonePresenter@Injectconstructor(privatevalpreference:DataPreference,privatevalioScheduler:Scheduler=Schedulers.io())ioScheduler是默认参数。我只想注入(inject)preference:DataPreference。现在我有异常(exception)[dagger.android.AndroidInjector.inject(T)]io.reactivex.Schedulercannotbeprovidedwithoutan@Provid

android - Dagger 2注入(inject)方式

我正在玩弄Dagger2.我有以下模块:@ModulepublicclassGameSetupModule{@Provides@SingletonGameSetupprovideGameSetup(){returnnewGameSetup();}}和相应的组件:@Singleton@Component(modules={GameSetupModule.class})publicinterfaceGameSetupComponent{GameSetupprovideGameSetup();voidinject(SetupActivityactivity);//voidinject(Fra

android - Java 的 Dagger 中的 addTo 和 includes 有什么区别?

我不确定有什么区别?我什么时候应该使用哪个?http://square.github.io/dagger/javadoc/index.html 最佳答案 includes表示当前模块由哪些模块组成。例如,它对于静态聚合所有模块很有用:@Module(includes={AndroidModule.class,NetworkModule.class,StorageModule.class})publicclassRootModule(){}//otherfileobjectGraph=ObjectGraph.create(newRoo

java - "inject everything"在 Android 中是一种不好的做法吗?

研究依赖注入(inject),我发现了一些建议injecteverything的方法和其他人说it'snotnecessarytodoso.在我当前的项目中,我关于依赖注入(inject)的经验法则是“如果类是我创建的,我就让它可注入(inject)”。换句话说,只有像SimpleDateFormat、ArrayList、HashMap这样的类在我的项目中是新的。我这样做的目的是,一旦在Activity中调用Injector.getApplicationComponent().inject(this),我就可以在任何地方@Inject任何类.基本上我所有的类都有一个带有@Inject的