问题
I want to share the same Viewmodel between my base fragment along with opened BottomSheetDialogFragment
So this is how i observe to same viewmodel between these two fragment.
BottomSheetDialogFragment
public class TasteFilterBottomDialogFragment extends BottomSheetDialogFragment {
private FilterTasteListViewModel filterTasteListViewModel;
@Inject
ViewModelProvider.Factory viewModelFactory;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
filterTasteListViewModel = ViewModelProviders.of(getActivity(),
viewModelFactory).get(FilterTasteListViewModel.class);
filterTasteListViewModel.init();
observeViewModel(filterTasteListViewModel);
}
private void observeViewModel(FilterTasteListViewModel filterTasteListViewModel) {
filterTasteListViewModel.getFilterTastes().observe(this, new Observer<ArrayList<String>>() {
@Override
public void onChanged(@Nullable ArrayList<String> tasteList) {
// Only this one trigger when viewModel is updated
Log.d("Bottom Dialog", tasteList.toString());
filterTasteList = tasteList;
setFilterButtonState(filterTasteList);
}
});
}
}
My base Fragment
public class FoodListFragment extends BaseFragment {
private static TasteFilterBottomDialogFragment tasteFilterBottomDialogFragment;
@Inject
ViewModelProvider.Factory viewModelFactory;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
showFoodListFilterButton();
if (mFoodList != null) {
mFoodListAdapter.setFoodList(mFoodList);
}
final FilterTasteListViewModel filterTasteListViewModel = ViewModelProviders.of(getActivity(),
viewModelFactory).get(FilterTasteListViewModel.class);
filterTasteListViewModel.init();
observeFilterTasteListViewModel(filterTasteListViewModel);
}
private void observeFilterTasteListViewModel(FilterTasteListViewModel filterTasteListViewModel) {
filterTasteListViewModel.getFilterTastes().observe(this, new Observer<ArrayList<String>>() {
@Override
public void onChanged(@Nullable ArrayList<String> filterTasteList) {
// This only trigger on the first time that fragment is initialise
Log.d("FoodList", filterTasteList.toString());
}
});
}
}
My ViewModel
public class FilterTasteListViewModel extends AndroidViewModel {
private LiveData<ArrayList<String>> filterTastesObservable;
public LiveData<ArrayList<String>> getFilterTastes() {
return this.filterTastesObservable;
}
public void setFilterTastes(ArrayList<String> filterTastes) {
((MutableLiveData<ArrayList<String>>) this.filterTastesObservable).postValue(filterTastes);
}
}
So when i called setFilterTastes
only this line is being called
Log.d("Bottom Dialog", tasteList.toString());
But actually this line should being called as well (but it is not).
Log.d("FoodList", filterTasteList.toString());
My question is how can i make the observable inside my baseFragment working same as inside my BottomSheetDialogFragment?
Thanks!
来源:https://stackoverflow.com/questions/55561333/share-viewmodel-between-bottomsheetdialogfragment-with-normal-fragment-not-worki