dagger-2

Dagger 2 - Injecting from multiple independently scoped components into a single object

痴心易碎 提交于 2019-12-23 17:25:02
问题 The issue that I'm facing is that I want to have two independent scopes that don't really fall into a parent-child hierarchy. In my case, I want two types of scopes: 1) "Feature" based scopes. e.g., when a user enters a feature, a scoped component is created. When the user leaves that feature, that scope is destroyed. 2) "Activity" based scopes (this is for an Android app, sorry about the terminology if you don't use Android). When an activity is created, a scoped component is created. When

Why do Dagger components have to declare their scope?

人走茶凉 提交于 2019-12-23 12:02:52
问题 Why do I have to annotate a Dagger component with the scopes it's going to use? Why is it not enough to annotate the class itself? 回答1: Because scopes by themselves don't mean anything. It's the components and their relationships that introduce meaning to scopes. Unscoped objects can be provided from any component if their dependencies are available. Since they are unscoped there will be a new object created every time that you call their provider. A scoped object will share the lifecycle of

Order of dependency injection when using scopes

守給你的承諾、 提交于 2019-12-23 11:56:07
问题 I'm currently trying to figure out Dagger 2. I am trying to set up 4 scopes: App, User, Activity, Fragment. User and Activity components are Subcomponents of App. Fragment is a Component with Activity as its dependency. Say my UserSettingsActivity needs a Toolbar (provided by ActivityModule), and a UserProfile (provided by UserModule). I won't get a UserProfile until I ask for it from the database, whereas the Toolbar can be provided right away. So the order of injection that takes place is

java.lang.IllegalArgumentException: No injector factory bound for Class<MyActivity_>

人走茶凉 提交于 2019-12-23 08:54:03
问题 The error I have is following: Caused by: java.lang.IllegalArgumentException: No injector factory bound for Class. Injector factories were bound for supertypes of MyActivity_: [MyActivity]. Did you mean to bind an injector factory for the subtype? As I understand it happens because I am using an AndroidAnnotations library. AppComponent.class : @Singleton @Component(modules = { AndroidInjectionModule.class, AppModule.class, ActivityBindingModule.class }) public interface AppComponent extends

Dagger-android build errror cannot be provided without an @Provides-annotated method

淺唱寂寞╮ 提交于 2019-12-23 05:34:05
问题 I am learning dagger dependency injection(I know i am late to the party, its better to start now). But I am facing below error. Please any help or suggestion could be greatly appreciated. ERROR Log error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] @com.mypackage.di.key.ItemDetail java.lang.Integer cannot be provided without an @Provides-annotated method. public interface ApplicationComponent extends AndroidInjector<RetailShopApplication> { ^ @com.mypackage.di.key

How to @Inject members in BaseActivity using dagger.android?

耗尽温柔 提交于 2019-12-23 03:52:08
问题 This is my scenario: I want all my Activities to inherit from BaseActivity Within the BaseActivity , I want to inject Navigator (it helps me to manage the Fragments backstack and navigate between activities): abstract class BaseActivity : DaggerAppCompatActivity() { @Inject lateinit var navigator: Navigator @Inject lateinit var prefs: SharedPreferences // injected via AppModule.kt, see below. } The Navigator class needs a FragmentManager in its constructor: class Navigator @Inject constructor

How to @Inject members in BaseActivity using dagger.android?

邮差的信 提交于 2019-12-23 03:51:59
问题 This is my scenario: I want all my Activities to inherit from BaseActivity Within the BaseActivity , I want to inject Navigator (it helps me to manage the Fragments backstack and navigate between activities): abstract class BaseActivity : DaggerAppCompatActivity() { @Inject lateinit var navigator: Navigator @Inject lateinit var prefs: SharedPreferences // injected via AppModule.kt, see below. } The Navigator class needs a FragmentManager in its constructor: class Navigator @Inject constructor

Kotliin Dagger Field Injection in ViewModel Throws Dagger/Binding Exception

牧云@^-^@ 提交于 2019-12-22 17:49:22
问题 I have followed this tutorial in order to do DI in my viewmodels. But I currently am stuck. I have created a ViewModelFactory for my viewmodel which is as follows: class HomeViewModelFactory @Inject constructor( private val creators: Map<Class<out ViewModel>, Provider<ViewModel>> ): ViewModelProvider.Factory{ override fun <T : ViewModel?> create(modelClass: Class<T>): T { return creators[modelClass]?.get() as T } } Followed by a ViewModel: class HomeViewModel @Inject constructor(private val

Dagger 2 - Lack of Constructor Injection example

☆樱花仙子☆ 提交于 2019-12-22 12:32:13
问题 I spend several hours for searching but I still don't find any clearly example about Constructor Injection with Dagger 2. Assume I have below declaration, how can I create an instance of class B somewhere using Dagger 2 injection? @Module public class ClassA { @Provides public ClassA provideA(){ return new ClassA(); } } public class ClassB{ private ClassA a; @Inject public ClassB(ClassA a){ this.a = a; } } 回答1: If you have @Module public class ModuleA { /*unscoped*/ @Provides public ClassA

Accessing strings.xml from ViewModel

心已入冬 提交于 2019-12-22 10:41:32
问题 I am using Dagger 2 DataBindng and the new Android Lifecycle components, which have ViewModels . Inside my ViewModel how could I get access to my strings.xml? I was thinking at first, to inject a Context into the viewModel , however, this will just leak memory. Are there any other ways? 回答1: There is an AndroidViewModel, which receives Application instance as parameter. From docs: Application context aware ViewModel . Subclasses must have a constructor which accepts Application as the only