I am working on Android application. I am using Dagger2 for dependency injection. I am able to use this library but I don't know how to inject nested classes.
public class Parent {
@Inject
public Parent()
{}
public String getParent() {
return "fifth";
}
class ParentSubClass{
@Inject
public ParentSubClass(){
}
public String getParentSubClass(){
return "subfifth";
}
}
}
class SomeTest{
@Inject
Parent.ParentSubClass subclass;
}
I know injecting parent class but how to create object for parentsubclass
@Inject
constructors are not supported on inner classes as Dagger tells us in the logcat:
error: @Inject constructors are invalid on inner classes public ParentSubClass()
If you'd like to provide your inner class, you should provide it using a @Provides
method:
@Provides
internal fun provideSubclass() : ParentSubClass {
return Parent().ParentSubClass()
}
该示例中的嵌套类可以访问其所有父级属性,因此您可以在父级中注入所需的所有依赖项,然后在子类中使用它们。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.