2 回答

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
要注入構(gòu)造函數(shù),Dagger 必須知道從哪里獲取傳遞給它的參數(shù),即您必須提供 HomeView homeView, HomeInteractor homeInteractor
因此,還要?jiǎng)?chuàng)建用于提供其他依賴項(xiàng)的方法:
@Provides
static HomeView provideHomeView() {
return ...
}
@Provides
static HomeInteractor provideHomeInteractor() {
return ...
}

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊
我對(duì) dagger 2 的 android 擴(kuò)展了解不多,但據(jù)我所知,有兩種方法可以實(shí)現(xiàn)您正在尋找的結(jié)果。
在相關(guān)組件中,您可以使用您的類型指定一個(gè)方法:
interface SomeComponent {
HomePresenter presenter(); // Method name does not matter here, only the type
}
并像這樣訪問它
class Home {
HomePresenter presenter;
void initialize() { //This could be your onCreate or wherever you typically inject
presenter = getSomeComponent().presenter();
}
}
或者,如果您為 Home 對(duì)象指定了一個(gè)注入方法,則可以請(qǐng)求它:
interface SomeComponent {
void inject(Home home);
}
class Home {
@Inject HomePresenter presenter;
void initialize(){
getSomeComponent().inject(this);
}
}
在這兩種情況下,您都必須確保Component包含適當(dāng)?shù)腗odules。
添加回答
舉報(bào)