我有一个活动和几个片段。
的活动:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
performInjection();
performDataBinding();
}
private void performDataBinding() {
viewDataBinding = DataBindingUtil.setContentView(this, setLayoutId());
this.viewModel = viewModel == null ? onCreateViewModel() : viewModel;
viewDataBinding.setVariable(setBindingVariable(), viewModel);
viewDataBinding.executePendingBindings();
}
@Override
public MainViewModel onCreateViewModel() {
return ViewModelProviders.of(this, viewModelFactory).get(MainViewModel.class);
}
DI注射:
@Provides
ViewModelProvider.Factory mainViewModelProvider(MainViewModel mainViewModel) {
return new ViewModelProviderFactory<>(mainViewModel);
}
片段代码:
@Override
public BalanceViewModel setViewModel() {
return ViewModelProviders.of(this, viewModelFactory).get(BalanceViewModel.class);
}
当我改变方向时记录:
D/BalanceViewModel: Recreation / constructor
D/MainViewModel: Recreation / constructor
D/BalanceViewModel: Recreation / constructor
D/BalanceViewModel: Recreation / onCleared
所以,我的问题是每次改变方向时viewModels调用构造函数的原因?是否与DI相关?