我需要使实例脱离我的对象。我为此使用了Dagger 2。
为此,我做到了:
public class OfflineCommonData {
private MainActivityViewModel mainActivityViewModel;
public OfflineCommonData(Object viewModel){
if(viewModel instanceof MainActivityViewModel)
mainActivityViewModel = (MainActivityViewModel) viewModel;
}
public MainActivityViewModel getMainActivityViewModel() {
return mainActivityViewModel;
}
public void setMainActivityViewModel(MainActivityViewModel mainActivityViewModel) {
this.mainActivityViewModel = mainActivityViewModel;
}
}
AppModule:
@Singleton @Provides
OfflineCommonData provideOfflineCommonData(Object viewModel){ return new OfflineCommonData(viewModel); }
最后我的接待:
@Inject
public OfflineDataViewModel(@NonNull Application application, @NonNull DataManager dataManager, @NonNull OfflineCommonData offlineCommonData) {
super(application);
this.dataManager = dataManager;
this.offlineCommonData = offlineCommonData;
我知道了
Cannot resolve symbol 'DaggerAppComponent'.
有帮助吗?