I initialize my ViewModel in the Activity with several fragments as tabs, but the fragments(tabs) return null for the updated livedata

20 views Asked by At

I have an activity with four fragments as tabs. Now I have a ViewModel (FJViewModel) for sharing filters between the activity and the fragments(tabs). In my activity, I initialize the filters table with four rows like this (for simplicity unrelated details are ommitted):

  private FJViewModel fjViewModel;
  Filterj filterj;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      fjViewModel = new ViewModelProvider(this).get(FJViewModel.class);
           .
           .

      fjViewModel.getAllFilters().observe(this, new Observer<List<Filterj>>() {
            @Override
            public void onChanged(List<Filterj> filterjs)
            {
                if (filterjs.size() == 0)
                {
                        filterj = new Filterj();        
                        filterj.setId(0);  //id=0 is for tab0
                        filterjs.add(filterj);
                        fjViewModel.insert(filterjs.get(0));

                        filterj = new Filterj();        
                        filterj.setId(1); //id=1 is for tab1
                        filterjs.add(filterj);
                        fjViewModel.insert(filterjs.get(1));
                    }
                }
            }
        });
            .
            .
  }

Inside my fragment (tab0) this is how I try to get the initial filters from the FJViewModel, but i get anull value inside the observer for the filterj. Although I checked hat the filters were changed inside the onChanged methos in the activity above.

 @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
      fjViewModel = new ViewModelProvider(requireActivity()).get( FJViewModel.class);
      fjViewModel.getFiltersById(0).observe(getViewLifecycleOwner(), new Observer<Filterj>() {
            @Override
            public void onChanged(Filterj filterj)
            {
                //Here the filterj is null when I check it
                filterj.setWithSoftware(false);
                fjViewModel.update(filterj);
             }
        });
           

And this is my FJViewModel (all columns except the id in the Filterj Entity are nullable in the room databse):

public class FJViewModel extends AndroidViewModel
{
    private FJRepository repo;
    private LiveData<List<Filterj>> allFilters;

    public FJViewModel(@NonNull Application application)
    {
        super(application);
        repo = new FJRepository(application);
        allFilters = repo.getAllFilters();
    }

    public void insert(Filterj filter) {
        repo.insert(filter);
    }
    public void update(Filterj filter) {
        repo.update(filter);
    }
    public void delete(Filterj filter) {
        repo.delete(filter);
    }
    public LiveData<List<Filterj>> getAllFilters(){
        return allFilters;
    }
    public LiveData<Filterj> getFiltersById(int id){
        return repo.getFiltersById(id);
    }
}

Now I am a bit confused as I am new to LiveData, why should i get a null for the initial value of the filterj? Also, when I change the filterj with e.g. a Spinner inside the fragment this problem goes away and I can clearly get the filters right inside my onItemSelected of the Spinner.

Where am I going wrong?

0

There are 0 answers