构造后,是否可以将数据传递到Dagger 2模块中? 例如,在我的应用中,我有一个Dagger 2 ActivityModule,在构造模块时,我在其中设置了全局变量:
@Module
public class ActivityModule {
//ActivityModule used for Dagger2
//Provide other classes/objects with access to certain Activity Components listed within the module
private SimulatorPresenter presenter;
private final SimulatorModel model;
private final ContentResolver contentResolver;
private final SharedPreferences sharedPreferences;
private final Context context;
public ActivityModule(HomeScreen homeScreen) {
this.model = new SimulatorModel();
this.contentResolver = homeScreen.getContentResolver();
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(homeScreen);
this.context = homeScreen;
}
对于此模块,SimulatorPresenter全局对象在构造时无法传递到模块中,因为它需要创建另一个活动。因此,在创建其他活动之后,是否可以使用dagger2在活动模块中手动设置SimulatorPresenter对象,还是需要创建一个全新的模块/组件?
构建模块的示例:
@Override
protected void onCreate(Bundle savedInstanceState) {
initializeTheme();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
mActivityComponent = DaggerActivityComponent.builder().activityModule(new ActivityModule(this)).build();
mActivityComponent.inject(this);