Share Viewmodel between BottomSheetDialogFragment with normal Fragment not working (observe function not trigger)

∥☆過路亽.° 提交于 2020-12-05 12:15:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!