I have a case where I have BottomNavigationView where fragments are shown/hidden instead of adding/replacing, therefore they do not go through their lifecycle every time.
Fragment 1 is observing one database table, Fragment 2 is observing a different one.
My goal is to call the onChanged of Fragment 2 when onChanged of Fragment 1 is called.
A stupid and naive solution that worked was to set up the observer of Fragment 1 in Fragment 2 and inside it call another observer:
mFragment1ViewModel1.getData().observe(this, new Observer<Fragment1Data>() {
@Override
public void onChanged(Fragment1Data fragment1Data) {
if(fragment1Data != null){
mFragmentViewModel2.getData().observe(SomeClass.this, new Observer<Fragment2Data>() {
@Override
public void onChanged(@Nullable Fragment2Data fragment2Data) {
// Do some stuff...
}
});
}
}
});
Could someone tell me what would be a good solution in this case and the implications of the solution mentioned above?